I'm trying to delete all stacks those environments(instances) were terminated. while passing the stack_name to the delete_stack it's throwing an error. I've tried to add the special characters (such as '') to the value of stack_name,but there was no luck. Can someone please help me to fix the issue. Thanks in advance!
#!/usr/bin/env python
import boto
import boto.ec2
import boto.cloudformation
import re
from datetime import datetime, timedelta
utclast = datetime.utcnow() - timedelta(2)
conn = boto.cloudformation.connect_to_region('us-west-1',aws_access_key_id = '<access_key>',aws_secret_access_key = '<secret_key>')
conn_ec2 = boto.ec2.connect_to_region('us-west-1',aws_access_key_id = '<access_key>',aws_secret_access_key = '<secret_key>')
stacks = conn.list_stacks()
for stackSumm in stacks:
pattern = re.compile("Testupload-env([a-zA-Z0-9]+)")
match = pattern.match(stackSumm.stack_name)
if stackSumm.stack_status in "CREATE_COMPLETE" and match and stackSumm.stack_name in match.string:
m = re.split(r'Testupload-', stackSumm.stack_name)
instance = conn_ec2.get_all_instances(filters={"tag:Name": m[1]})
if not instance:
try:
print "Trying to delete stack: %s" % stackSumm.stack_name
conn.delete_stack(stackSumm.stack_name)
except boto.exception.BotoServerError, e:
print e.error_message
Error:
File "delete_stack.py", line 7, in <module>
conn.delete_stack(Testupload-envmeraleb8b01739116b0f36d17a2b5445b949f592bb625-6293)
NameError: name 'Testupload' is not defined
None of you posted code generated the error, but the NameError says that you didn't provide a valid stack name
delete_stack(stack_name_or_id)
Deletes a specified stack. Once the call completes successfully, stack deletion starts. Deleted stacks do not show up in the DescribeStacks API if the deletion has been completed successfully.
Parameters: stack_name_or_id (string) – The name or the unique identifier associated with the stack.
The name/id should be a string, a string in python must be quoted, either by single quotes or double quotes
stack_name = "Testupload-envmeraleb8b01739116b0f36d17a2b5445b949f592bb625-6293"
conn.delete_stack(stack_name)
Related
I'm writing a Python script that logs into a server and pulls router data through an api. The pulled data is then used to create a dictionary with router names as the key and a telnet url as the key. Here is an example of the url that's collected.
telnet://192.168.1.113:32769
The telnet port is the last 5 characters of the url and I'm trying to pull that information only. I know with a string I can use (-5) but I'm getting the following error.
Traceback (most recent call last):
File "C:\Users\b\Documents\Atom Test1 Project\test_wip.py", line 41, in <module>
test_value2=test_value.split(-5)
TypeError: must be str or None, not int
[Finished in 1.812s]
I think this means I need to convert it tonto a string. I tried converting and then retrieving the last 5 charcters but it's not working. Here is my code.
from __future__ import unicode_literals, print_function
import eve
import json
import time
from netmiko import ConnectHandler, redispatch
#from resteve import eve
import json
address = '192.168.1.113'
instance = m11.Server(address)
instance.login('admin', 'password', '0')
users = instance.get_all_nodes()
payload = json.loads(users.content)
data = payload['data']
users = instance.get_all_nodes()
payload = json.loads(users.content)
data = payload['data']
for item in payload["data"].values():
result[item["name"]] = item["url"]
test_value=item['url']
print(test_value)
test_value.format(str)
test_value2=test_value.split(-5)
print(test_value2)
I'm new at this and still putting it all together so any help is greatly appreciated. Thanks.
To get last 5 chars use indexing test_value[-5:], because .split() expects a string and here it will try to split on the first argument
Using cx_Oracle, I am trying to use a Python script to execute a sql command, using the bind variables 'plat' and 'tick'. When trying to execute this command, it gives me the error "ORA-24373: invalid length specified for statement".
To debug, I made a SQL call through Oracle (not Python) using the same arguments as my script (plat=1234567, tick='ABCDE'), and it ran as expected. I tried passing the parameters as both a dict and individual named variables, but both times I got the same error.
I tried changing the values to be lower ('1' and 'A'), but even that is an 'invalid length'.
updateRecords.py
import os
import cx_Oracle
# For security reasons I cannot show my 'create_connection()' function,
# but suffice to say I have tested it and it works as desired.
...
#Setup:
WORKING_PATH = os.path.dirname(os.path.abspath(__file__))
SQL_PATH = os.path.join(WORKING_PATH, 'sql')
cnnSB = create_connection()
cnnSB_cursor = cnnSB.cursor()
...
fetchComp = open(os.path.join(SQL_PATH, 'fetchRecentEntry.sql'), 'r')
for x in range(0, 5):
cnnSB_cursor.execute(fetchComp.read(), {"plat":'A', "tick":1}) # ERROR LINE
fetchRecentEntry.sql
select *
from MFS_PCIINCEXTFUNDBYPLAT
where PLATFORM = :plat
and TICKER = :tick
and STARTDATE = (select max(STARTDATE) from MFS_PCIINCEXTFUNDBYPLAT
where PLATFORM = :plat
and TICKER = :tick)
The above snippet results in the following error message:
File "updateRecords.py", line 297, in main
cnnSB_cursor.execute(fetchComp.read(), plat='A', tick=1)
cx_Oracle.DatabaseError: ORA-24373: invalid length specified for statement
Other things I have checked:
-My fetchComp.read() DOES return the desired code
-Passing in variables as a dict object does NOT change the error message
I found a solution:
The issue comes from the .read() being called inside of a loop. As a result, it would read the file correctly the first time, but on subsequent loops it would only read the null/EOF.
To fix, all I had to do was set the sql.read() to a variable before the loop, and use that variable instead of calling .read() with each loop.
Example:
sql = fetchComp.read()
for index, testRow in testDF.iterrows():
cnnSB_cursor.execute(sql, tick=testRow[1], plat=testRow[0])
compDF = pd.DataFrame(cnnSB_cursor.fetchall())
I've looked through a lot of replies regarding this error, however none was helpfull for my special case and since I'm new to Python, I have difficulties applying the hints to my problem.
I have a class in a file Aheat.py that reads
class Aheat():
name = ""
time = 0
place = 0
def __init__(self,name,time,place):
self.name = name
self.time = time
self.place = place
And a file main.py where I want to read a html file, extract information, and create a list of objects of my class to work with them later on.
The (hopefully) essential part of my main.py reads
import urllib2
import re
from Aheat import Aheat
s = read something from url
ssplit = re.split('<p', s) # now every entry of ssplit contains an event
# and description and all the runners
HeatList = []
for part in ssplit:
newHeat = Aheat("foo",1,1) # of course this is just an example
HeatList.append(newHeat)
But this gives me the following error:
Traceback (most recent call last):
File "/home/username/Workspace/ECLIPSE/running/main.py", line 22, in <module>
newHeat = Aheat("foo",1,1)
TypeError: 'list' object is not callable
which is thrown when performing the second iteration.
If I take out the generation of the object of the loop, i.e.
newHeat = Aheat("foo",1,1)
for part in ssplit:
HeatList.append(newHeat)
My code executes without a problem, but this is not what I want. I'm also not sure, if I can initialize a specific number of instances a priori, since the number of objects is estimated in the loop.
I'm using Eclipse and Python 2.7.
regex is going to bite you.
<p == <pre> || <progress> || <param> || <p> || (any user created directives on a page.)
follow the links in your comments to read up on why we shouldn't parse html with regex.
Thanks, #MarkR ( btw, I was only supplementing your comment and I was agreeing with you )
Why not put the list in your class or better yet extend list functionality with your class.
class AHeat(list):
def append(self,name,time,place):
return super(AHeat,self).append([name,time,place])
# main
heatList= AHeat()
heatList.append("foo",1,2)
heatList.append("bar",3,4)
print(heatList[0])
print(heatList[1])
> ['foo', 1, 2]
> ['bar', 3, 4]
Also
I am trying to make a custom command to run on my celery crontabs, which extracts data from my db, makes a list, and then dumps that information as a list in redis. This list will then be used by other workers.
However, I am getting a NameError, which I haven't been able to solve despite reading relevant stack and Google posts.
My code below:
from django.core.management.base import NoArgsCommand, CommandError
from detail.models import SD
import redis
class Command(NoArgsCommand):
help = 'Gathers the symbols from the database and generates a list for crontabs, saving to redis.'
def handle_noargs(self, **options):
all = SD.objects.all()
data = []
for info in all:
data.append(info.symb)
r = redis.Redis()
try:
r.delete('allsymbols')
except:
pass
for xyz in data: **<---- the NameError refers to this line**
r.rpush('allsymbols', xyz)
TRACEBACK
File "C:\.....command.py", line 5, in <module>
class Command(NoArgsCommand):
File "C:\.....command.py", line 26, in Command
for xyz in data:
NameError: name 'data' is not defined
Script contains mixed tabs and spaces.
You should rather use only spaces, as per pep8, but the choice is yours, as long as you use the same in all your scripts.
I'm trying to create a simple module for phenny, a simple IRC bot framework in Python. The module is supposed to go to http://www.isup.me/websitetheuserrequested to check is a website was up or down. I assumed I could use regex for the module seeing as other built-in modules use it too, so I tried creating this simple script although I don't think I did it right.
import re, urllib
import web
isupuri = 'http://www.isup.me/%s'
check = re.compile(r'(?ims)<span class="body">.*?</span>')
def isup(phenny, input):
global isupuri
global cleanup
bytes = web.get(isupuri)
quote = check.findall(bytes)
result = re.sub(r'<[^>]*?>', '', str(quote[0]))
phenny.say(result)
isup.commands = ['isup']
isup.priority = 'low'
isup.example = '.isup google.com'
It imports the required web packages (I think), and defines the string and the text to look for within the page. I really don't know what I did in those four lines, I kinda just ripped the code off another phenny module.
Here is an example of a quotes module that grabs a random quote from some webpage, I kinda tried to use that as a base: http://pastebin.com/vs5ypHZy
Does anyone know what I am doing wrong? If something needs clarified I can tell you, I don't think I explained this enough.
Here is the error I get:
Traceback (most recent call last):
File "C:\phenny\bot.py", line 189, in call
try: func(phenny, input)
File "C:\phenny\modules\isup.py", line 18, in isup
result = re.sub(r'<[^>]*?>', '', str(quote[0]))
IndexError: list index out of range
try this (from http://docs.python.org/release/2.6.7/library/httplib.html#examples):
import httplib
conn = httplib.HTTPConnection("www.python.org")
conn.request("HEAD","/index.html")
res = conn.getresponse()
if res.status >= 200 and res.status < 300:
print "up"
else:
print "down"
You will also need to add code to follow redirects before checking the response status.
edit
Alternative that does not need to handle redirects but uses exceptions for logic:
import urllib2
request = urllib2.Request('http://google.com')
request.get_method = lambda : 'HEAD'
try:
response = urllib2.urlopen(request)
print "up"
print response.code
except urllib2.URLError, e:
# failure
print "down"
print e
You should do your own tests and choose the best one.
The error means your regexp wasn't found anywhere on the page (the list quote has no element 0).