def credentials(self): wrong_url = "E: Invalid target URL. Try with something like https://conpaas.example.com:5555\n" # Loop till we get a valid URL while True: try: # Previously saved target_url, if any target = self.read_conf_value("target") except IOError: target = '' target = rlinput('Enter the director URL: ', target) try: url = urlparse.urlparse(target) except IndexError: print wrong_url continue if url.scheme != "https": print wrong_url continue # Check if a ConPaaS director is listening at the provided URL try: available_services = self.__callapi_creds( method='available_services', post=False, data={}, endpoint=target, use_certs=False) # If this yields True we can be reasonably sure that the # provided URL is correct assert type(available_services) is list except Exception, e: print "E: No ConPaaS Director at the provided URL: %s\n" % e continue # Valid URL self.write_conf_to_file('target', target) break
def download_manifest(self, appid): services = self.callapi("list/%s" % appid, True, {}) for service in services: if service['type'] == 'xtreemfs': warning = """WARNING: this application contains an XtreemFS service After downloading the manifest, the application will be deleted Do you want to continue? (y/N): """ sys.stderr.write(warning) sys.stderr.flush() confirm = '' confirm = rlinput('', confirm) if confirm != 'y': sys.exit(1) res = self.callapi("download_manifest/%s" % appid, True, {}) if res: print simplejson.dumps(res) else: print "E: Failed downloading manifest file"
def download_manifest(self, appid): services = self.callapi("list/%s" % appid, True, {}) for service in services: if service["type"] == "xtreemfs": warning = """WARNING: this application contains an XtreemFS service After downloading the manifest, the application will be deleted Do you want to continue? (y/N): """ sys.stderr.write(warning) sys.stderr.flush() confirm = "" confirm = rlinput("", confirm) if confirm != "y": sys.exit(1) res = self.callapi("download_manifest/%s" % appid, True, {}) if res: print simplejson.dumps(res) else: print "E: Failed downloading manifest file"
class BaseClient(object): # Set this to the service type. eg: php, java, mysql... service_type = None def __init__(self): self.confdir = os.path.join(os.environ['HOME'], ".conpaas") if not os.path.isdir(self.confdir): os.mkdir(self.confdir, 0700) try: https.client.conpaas_init_ssl_ctx(self.confdir, 'user') except IOError: # We do not have the certificates yet. But we will get them soon: # see getcerts() pass def write_conf_to_file(self, key, value): targetfile = open(os.path.join(self.confdir, key), 'w') targetfile.write(value) targetfile.close() def read_conf_value(self, key): return open(os.path.join(self.confdir, key)).read() def __callapi_creds(self, method, post, data, endpoint, username='', password='', use_certs=True): url = "%s/%s" % (endpoint, method) data['username'] = username data['password'] = password data = urllib.urlencode(data) if use_certs: opener = urllib2.build_opener(HTTPSClientAuthHandler( os.path.join(self.confdir, 'key.pem'), os.path.join(self.confdir, 'cert.pem'))) else: opener = urllib2.build_opener(urllib2.HTTPSHandler()) if post: res = opener.open(url, data) else: url += "?" + data res = opener.open(url) rawdata = res.read() try: res = simplejson.loads(rawdata) if type(res) is dict and res.get('error') is True: raise Exception(res['msg'] + " while calling %s" % method) return res except simplejson.decoder.JSONDecodeError: return rawdata def callapi(self, method, post, data, use_certs=True): """Call the director API. 'method': a string representing the API method name. 'post': boolean value. True for POST method, false for GET. 'data': a dictionary representing the data to be sent to the director. callapi loads the director JSON response and returns it as a Python object. If the returned data can not be decoded it is returned as it is. """ try: endpoint = self.read_conf_value("target") username = self.read_conf_value("username") password = self.read_conf_value("password") except IOError: self.credentials() return self.callapi(method, post, data, use_certs) try: return self.__callapi_creds(method, post, data, endpoint, username, password, use_certs) except (ssl.SSLError, urllib2.URLError): print "E: Cannot perform the requested action.\nTry updating your client certificates with %s credentials" % sys.argv[0] sys.exit(1) def callmanager(self, service_id, method, post, data, files=[]): """Call the manager API. 'service_id': an integer holding the service id of the manager. 'method': a string representing the API method name. 'post': boolean value. True for POST method, false for GET. 'data': a dictionary representing the data to be sent to the director. 'files': sequence of (name, filename, value) tuples for data to be uploaded as files. callmanager loads the manager JSON response and returns it as a Python object. """ service = self.service_dict(service_id) # File upload if files: res = https.client.https_post(service['manager'], 443, '/', data, files) # POST elif post: res = https.client.jsonrpc_post(service['manager'], 443, '/', method, data) # GET else: res = https.client.jsonrpc_get(service['manager'], 443, '/', method, data) if res[0] == 200: try: data = simplejson.loads(res[1]) except simplejson.decoder.JSONDecodeError: # Not JSON, simply return what we got return res[1] return data.get('result', data) raise Exception, "Call to method %s on %s failed: %s.\nParams = %s" % ( method, service['manager'], res[1], data) def wait_for_state(self, sid, state): """Poll the state of service 'sid' till it matches 'state'.""" res = { 'state': None } while res['state'] != state: try: res = self.callmanager(sid, "get_service_info", False, {}) except (socket.error, urllib2.URLError): time.sleep(2) def createapp(self, app_name): print "Creating new application... " if self.callapi("createapp", True, { 'name': app_name }): print "done." else: print "failed." sys.stdout.flush() def create(self, service_type, cloud = None, application_id=None, initial_state='INIT'): data = {} if application_id is not None: data['appid'] = application_id if cloud is None: res = self.callapi("start/" + service_type, True, data) else: res = self.callapi("start/" + service_type + '/' + cloud, True, data) sid = res['sid'] print "Creating new manager on " + res['manager'] + "... ", sys.stdout.flush() self.wait_for_state(sid, initial_state) print "done." sys.stdout.flush() def start(self, service_id, cloud = "default"): data = {'cloud': cloud} res = self.callmanager(service_id, "startup", True, data) if 'error' in res: print res['error'] else: print "Your service is starting up." def stop(self, service_id): print "Stopping service... " sys.stdout.flush() res = self.callmanager(service_id, "get_service_info", False, {}) if res['state'] == "RUNNING": print "Service is in '%(state)s' state. Shutting it down." % res res = self.callmanager(service_id, "shutdown", True, {}) else: print "Service is in '%(state)s' state. We can not stop it." % res def terminate(self, service_id): print "Terminating service... " sys.stdout.flush() res = self.callmanager(service_id, "get_service_info", False, {}) if res['state'] not in ( "STOPPED", "INIT" ): print "Service is in '%s' state. We can not terminate it." % res['state'] return res = self.callapi("stop/%s" % service_id, True, {}) if res: print "done." else: print "failed." def rename(self, service_id, newname): print "Renaming service... " if self.callapi("rename/%s" % service_id, True, { 'name': newname }): print "done." else: print "failed." def service_dict(self, service_id): """Return service's data as a dictionary""" services = self.callapi("list", True, {}) for service in services: if str(service['sid']) == str(service_id): service.pop('state') return service return [] def info(self, service_id): """Print service info. Clients should extend this method and print any additional information needed. Returns service_dict""" service = self.service_dict(service_id) for key, value in service.items(): print "%s: %s" % (key, value) res = self.callmanager(service['sid'], "get_service_info", False, {}) print "state:", res['state'] for key, value in res.items(): service[key] = value return service def logs(self, service_id): res = self.callmanager(service_id, "getLog", False, {}) print res['log'] def getcerts(self): res = self.callapi("getcerts", True, {}, use_certs=False) zipdata = zipfile.ZipFile(StringIO.StringIO(res)) zipdata.extractall(path=self.confdir) https.client.conpaas_init_ssl_ctx(self.confdir, 'user') #for name in zipdata.namelist(): # print os.path.join(self.confdir, name) def credentials(self): wrong_url = "E: Invalid target URL. Try with something like https://conpaas.example.com:5555\n" # Loop till we get a valid URL while True: try: # Previously saved target_url, if any target = self.read_conf_value("target") except IOError: target = '' target = rlinput('Enter the director URL: ', target) try: url = urlparse.urlparse(target) except IndexError: print wrong_url continue if url.scheme != "https": print wrong_url continue # Check if a ConPaaS director is listening at the provided URL try: available_services = self.__callapi_creds( method='available_services', post=False, data={}, endpoint=target, use_certs=False) # If this yields True we can be reasonably sure that the # provided URL is correct assert type(available_services) is list except Exception, e: print "E: No ConPaaS Director at the provided URL: %s\n" % e continue # Valid URL self.write_conf_to_file('target', target) break while True: try: # Previously saved username, if any username = self.read_conf_value("username") except IOError: username = '' # Get the username username = rlinput('Enter your username: '******'username', username) # Get the password password = getpass.getpass('Enter your password: '******'password', password) if self.callapi('login', True, {}, use_certs=False): print "Authentication succeeded\n" self.getcerts() return print "Authentication failure\n"
if __name__ == "__main__": db.create_all() credit = 50 try: args = sys.argv[1:] if '-h' in args or '--help' in args: print "Usage: %s email username password [credit=%s]" % (sys.argv[0], credit) exit(0) if len(args) == 3: email, username, password = args else: email, username, password, credit = args except ValueError: print "\nAdd new ConPaaS user" email = rlinput('E-mail: ') username = rlinput('Username: '******'Retype password: '******'Passwords do not match. Try again') password, p2 = pprompt() try: create_user(username, "", "", email, "", password, credit, "") # here we don't fill in: fname, lname, affiliation, uuid except sqlalchemy.exc.IntegrityError as e: print "User %s already present" % username
from distutils.version import StrictVersion CERT_DIR = common.config_parser.get('conpaas', 'CERT_DIR') if common.config_parser.has_option('director', 'DIRECTOR_URL'): # Get default hostname from DIRECTOR_URL if it exists already hostname = common.config_parser.get('director', 'DIRECTOR_URL') hostname = re.sub(':.*', '', urlparse(hostname).netloc) else: # If DIRECTOR_URL does not exist, just trust platform.node() hostname = platform.node() try: hostname = sys.argv[1] except IndexError: hostname = rlinput('Please enter your hostname: ', hostname) # create CA keypair cakey = x509.gen_rsa_keypair() # save ca_key.pem to filesystem open(os.path.join(CERT_DIR, 'ca_key.pem'), 'w').write(x509.key_as_pem(cakey)) # create cert request req = x509.create_x509_req(cakey, CN='CA', emailAddress='*****@*****.**', O='ConPaaS') five_years = 60 * 60 * 24 * 365 * 5 # create ca certificate, valid for five years cacert = x509.create_cert(
import getpass import sqlalchemy from cpsdirector import db, common from cpsdirector.user import create_user from conpaas.core.misc import rlinput if __name__ == "__main__": db.create_all() try: email, username, password = sys.argv[1:] except ValueError: print "\nAdd new ConPaaS user" email = rlinput('E-mail: ') username = rlinput('Username: '******'Retype password: '******'Passwords do not match. Try again') password, p2 = pprompt() try: create_user(username, "", "", email, "", password, 50) except sqlalchemy.exc.IntegrityError: print "User %s already present" % username
from distutils.spawn import find_executable CERT_DIR = common.config_parser.get('conpaas', 'CERT_DIR') if common.config_parser.has_option('director', 'DIRECTOR_URL'): # Get default hostname from DIRECTOR_URL if it exists already hostname = common.config_parser.get('director', 'DIRECTOR_URL') hostname = re.sub(':.*', '', urlparse(hostname).netloc) else: # If DIRECTOR_URL does not exist, just trust platform.node() hostname = platform.node() try: hostname = sys.argv[1] except IndexError: hostname = rlinput('Please enter your hostname: ', hostname) # create CA keypair cakey = x509.gen_rsa_keypair() # save ca_key.pem to filesystem open(os.path.join(CERT_DIR, 'ca_key.pem'), 'w').write(x509.key_as_pem(cakey)) # create cert request req = x509.create_x509_req(cakey, CN='CA', emailAddress='*****@*****.**', O='ConPaaS') five_years = 60 * 60 * 24 * 365 * 5