1
# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
# Copyright [2010] [Anso Labs, LLC]
4
# Licensed under the Apache License, Version 2.0 (the "License");
5
# you may not use this file except in compliance with the License.
6
# You may obtain a copy of the License at
8
# http://www.apache.org/licenses/LICENSE-2.0
10
# Unless required by applicable law or agreed to in writing, software
11
# distributed under the License is distributed on an "AS IS" BASIS,
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
# See the License for the specific language governing permissions and
14
# limitations under the License.
17
System-level utilities and helper functions.
28
def fetchfile(url, target):
29
logging.debug("Fetching %s" % url)
31
# fp = open(target, "wb")
32
# c.setopt(c.URL, url)
33
# c.setopt(c.WRITEDATA, fp)
37
execute("curl %s -o %s" % (url, target))
39
def execute(cmd, input=None):
40
#logging.debug("Running %s" % (cmd))
41
obj = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE,
42
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
45
result = obj.communicate(input)
47
result = obj.communicate()
50
logging.debug("Result was %s" % (obj.returncode))
54
return os.path.join(os.path.dirname(__file__), s)
56
def default_flagfile(filename='nova.conf'):
58
if arg.find('flagfile') != -1:
61
if not os.path.isabs(filename):
62
# turn relative filename into an absolute path
63
script_dir = os.path.dirname(inspect.stack()[-1][1])
64
filename = os.path.abspath(os.path.join(script_dir, filename))
65
if os.path.exists(filename):
66
sys.argv = sys.argv[:1] + ['--flagfile=%s' % filename] + sys.argv[1:]
69
logging.debug('debug in callback: %s', arg)
72
def runthis(prompt, cmd):
73
logging.debug("Running %s" % (cmd))
74
logging.debug(prompt % (subprocess.call(cmd.split(" "))))
77
def generate_uid(topic, size=8):
78
return '%s-%s' % (topic, ''.join([random.choice('01234567890abcdefghijklmnopqrstuvwxyz') for x in xrange(size)]))
81
mac = [0x00, 0x16, 0x3e, random.randint(0x00, 0x7f),
82
random.randint(0x00, 0xff), random.randint(0x00, 0xff)
84
return ':'.join(map(lambda x: "%02x" % x, mac))
86
def last_octet(address):
87
return int(address.split(".")[-1])
90
''' returns the actual ip of the local machine.
92
csock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
93
csock.connect(('www.google.com', 80))
94
(addr, port) = csock.getsockname()