Skip to content

Commit 017c909

Browse files
committed
Major Refactoring
1 parent 6ce5e44 commit 017c909

File tree

1 file changed

+1
-69
lines changed

1 file changed

+1
-69
lines changed

utils.py

Lines changed: 1 addition & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
import pwd
66
import uuid
77
import functools
8-
import boto3
98

109
def enum(*sequential, **named):
1110
enums = dict(zip(sequential, range(len(sequential))), **named)
@@ -95,74 +94,7 @@ def daemonize(pidfile, logfile=None, user='ubuntu', drop=True):
9594
sys.stdin.close()
9695
sys.stdout=sys.stderr=log
9796

98-
class LinodeCommand(object):
99-
""" Class encapsulating linode CLI commands """
100-
101-
def __init__(self, binary='linode', verbose=False, config=None):
102-
self.binary = binary
103-
self.verbose = verbose
104-
self.cmd_template = {'create': 'create -d %d -p %d -o %d -i %d -l %s -r %s',
105-
'delete': 'delete -l %d',
106-
'list_proxies': 'find -g %s -s %s' % (config.group, config.proxylb),
107-
'info': 'info -l %d',
108-
'update': 'update -l %d -L %s -g %s'
109-
}
110-
# Dynamically create command methods
111-
self.dyn_create()
112-
113-
def _run(self, command, *args):
114-
""" Run a command and return the output """
115-
116-
template = self.cmd_template.get(command)
117-
if template == None:
118-
print 'No such command configured =>',command
119-
return -1
120-
121-
cmd = ' '.join((self.binary, template % args))
122-
if self.verbose: print 'Command is',cmd
123-
return os.popen(cmd).read()
124-
125-
def dyn_create(self):
126-
""" Dynamically create linode methods """
127-
128-
for cmd in self.cmd_template:
129-
method_name = 'linode_' + cmd
130-
method = functools.partial(self._run, cmd)
131-
if self.verbose: print 'Dyn-creating method',method_name,'...'
132-
setattr(self, method_name, method)
133-
134-
def get_label(self, linode_id):
135-
""" Return the label, given the linode id """
136-
137-
data = self.linode_info(linode_id)
138-
return data.split('\n')[0].split(':')[-1].strip()
139-
140-
class AWSCommand(object):
141-
'''Class encapsulating the aws ec2 API'''
142-
def __init__(self, config=None):
143-
self.ec2 = boto3.resource('ec2')
144-
self.config = config
145-
146-
def create_ec2(self, **params):
147-
return self.ec2.create_instances(MaxCount=1, MinCount=1, **params)[0]
148-
149-
def list_proxies(self):
150-
proxies = []
151-
filters=[
152-
{'Name':'image-id', 'Values':[self.config.aws_image_id]},
153-
{'Name': 'instance-state-name', 'Values': ['running']}
154-
]
155-
for instance in self.ec2.instances.filter(Filters=filters):
156-
proxies.append(','.join([instance.public_ip_address, '0', instance.id,'0','0']))
157-
return proxies
158-
159-
def delete_ec2(self, instance_id):
160-
instance = self.ec2.Instance(instance_id)
161-
instance.terminate()
162-
instance.wait_until_terminated()
163-
16497
if __name__ == "__main__":
165-
l = LinodeCommand()
166-
l.get_label(int(sys.argv[1]))
98+
pass
16799

168100

0 commit comments

Comments
 (0)