def main(): module = AnsibleModule( argument_spec=dict( ssh_cert_path=dict(), name=dict(), hostname=dict(), os_type=dict(default='linux', choices=['linux', 'windows']), location=dict(choices=AZURE_LOCATIONS), role_size=dict(choices=AZURE_ROLE_SIZES), subscription_id=dict(no_log=True), storage_account=dict(), management_cert_path=dict(), endpoints=dict(default='22'), user=dict(), password=dict(), image=dict(), virtual_network_name=dict(default=None), state=dict(default='present'), wait=dict(type='bool', default=False), wait_timeout=dict(default=600), wait_timeout_redirects=dict(default=300), reset_pass_atlogon=dict(type='bool', default=False), auto_updates=dict(type='bool', default=False), enable_winrm=dict(type='bool', default=True), ) ) if not HAS_AZURE: module.fail_json(msg='azure python module required for this module') # create azure ServiceManagementService object subscription_id, management_cert_path = get_azure_creds(module) wait_timeout_redirects = int(module.params.get('wait_timeout_redirects')) if hasattr(windows_azure, '__version__') and LooseVersion(windows_azure.__version__) <= "0.8.0": # wrapper for handling redirects which the sdk <= 0.8.0 is not following azure = Wrapper(ServiceManagementService(subscription_id, management_cert_path), wait_timeout_redirects) else: azure = ServiceManagementService(subscription_id, management_cert_path) cloud_service_raw = None if module.params.get('state') == 'absent': (changed, public_dns_name, deployment) = terminate_virtual_machine(module, azure) elif module.params.get('state') == 'present': # Changed is always set to true when provisioning new instances if not module.params.get('name'): module.fail_json(msg='name parameter is required for new instance') if not module.params.get('image'): module.fail_json(msg='image parameter is required for new instance') if not module.params.get('user'): module.fail_json(msg='user parameter is required for new instance') if not module.params.get('location'): module.fail_json(msg='location parameter is required for new instance') if not module.params.get('storage_account'): module.fail_json(msg='storage_account parameter is required for new instance') if not (module.params.get('password') or module.params.get('ssh_cert_path')): module.fail_json(msg='password or ssh_cert_path parameter is required for new instance') (changed, public_dns_name, deployment) = create_virtual_machine(module, azure) module.exit_json(changed=changed, public_dns_name=public_dns_name, deployment=json.loads(json.dumps(deployment, default=lambda o: o.__dict__)))
def main(): """Main procedure for Azure monitor utility.""" global logger error = '' error_code = 0 args = handle_args() if not args.all and not args.storageact: print 'Storage acct name missing' sys.exit(3) setup_logger(args.verbose) logger.debug('Converting publishsettings.') try: publishsettings = PublishSettings(args.psfile) except error: print 'Publishsettings file is not good. Error %s' % error sys.exit(1) if os.name != 'nt': pem_path = publishsettings.write_pem() logger.debug('Pem file saved to temp file {0}'.format(pem_path)) logger.debug('Azure sub id {0}'.format(publishsettings.sub_id)) management = ServiceManagementService( subscription_id=publishsettings.sub_id, cert_file=pem_path) else: logger.debug('Using cert to instantiate ServiceManagement.') if args.cert: management = ServiceManagementService(publishsettings.sub_id, cert_file=args.cert) else: print 'Cert is missing. Required on Windows' sys.exit(3) if args.subtype != 'cap': if not args.key: print 'Key is required for storage transactions.' sys.exit(3) if args.all: error_code, error = check_storage_errors_all(management, args.type, args.subtype, args.key, args.warning, args.critical, args.verbose) else: error_code, error = check_storage_errors(management, args.storageact.lower(), args.type, args.subtype, args.key, args.warning, args.critical, args.verbose) if os.name != 'nt': os.unlink(pem_path) logger.debug('Deleted pem.') if error_code == 0 and not error: error = 'All OK' print error sys.exit(error_code)
def launch_node(node_id, creds, params, init, ssh_username, ssh_private_key): # "pip install azure" sms = ServiceManagementService( subscription_id='69581868-8a08-4d98-a5b0-1d111c616fc3', cert_file='/Users/dgriffin/certs/iOSWAToolkit.pem') for i in sms.list_os_images(): print 'I is ', i.name, ' -- ', i.label, ' -- ', i.location, ' -- ', i.media_link media_link = \ 'http://opdemandstorage.blob.core.windows.net/communityimages/' + \ 'b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu_DAILY_BUILD-' + \ 'precise-12_04_2-LTS-amd64-server-20130702-en-us-30GB.vhd' config = LinuxConfigurationSet(user_name="ubuntu", user_password="******") hard_disk = OSVirtualHardDisk( 'b39f27a8b8c64d52b05eac6a62ebad85__Ubuntu_DAILY_BUILD-' + 'precise-12_04_2-LTS-amd64-server-20130702-en-us-30GB', media_link, disk_label='opdemandservice') ret = sms.create_virtual_machine_deployment('opdemandservice', 'deploy1', 'production', 'opdemandservice2', 'opdemandservice3', config, hard_disk) # service_name, deployment_name, deployment_slot, label, role_name # system_config, os_virtual_hard_disk print 'Ret ', ret return sms
def setUp(self): self.sms = ServiceManagementService( credentials.getSubscriptionId(), credentials.getManagementCertFile()) set_service_options(self.sms) self.storage_account_name = getUniqueName('utstor')
def setUp(self): self.sms = ServiceManagementService( credentials.getSubscriptionId(), credentials.getManagementCertFile()) set_service_options(self.sms) self.certificate_thumbprints = []
def __init__(self): """Main execution path.""" # Inventory grouped by display group self.inventory = {} # Index of deployment name -> host self.index = {} # Read settings and parse CLI arguments self.read_settings() self.read_environment() self.parse_cli_args() # Initialize Azure ServiceManagementService self.sms = ServiceManagementService(self.subscription_id, self.cert_path) # Cache if self.args.refresh_cache: self.do_api_calls_update_cache() elif not self.is_cache_valid(): self.do_api_calls_update_cache() if self.args.list_images: data_to_print = self.json_format_dict(self.get_images(), True) elif self.args.list: # Display list of nodes for inventory if len(self.inventory) == 0: data_to_print = self.get_inventory_from_cache() else: data_to_print = self.json_format_dict(self.inventory, True) print data_to_print
def __init__(self, access_key): """ :param access_key: subscription_id #certificate_path the :return: cons """ self.certificate_path = os.path.join('../conf/', 'mycert.cer') self.sms = ServiceManagementService(access_key)
def generate_azure_service(self, azure_key_id): azure_key = self.db.get_object(AzureKey, azure_key_id) if self.azure_adapter is not None and self.azure_adapter.subscription_id == azure_key.subscription_id: return self.azure_adapter self.azure_adapter = ServiceManagementService( azure_key.subscription_id, azure_key.pem_url, azure_key.management_host) return self.azure_adapter
def setUp(self): proxy_host = credentials.getProxyHost() proxy_port = credentials.getProxyPort() self.sms = ServiceManagementService(credentials.getSubscriptionId(), credentials.getManagementCertFile()) if proxy_host: self.sms.set_proxy(proxy_host, proxy_port) self.management_certificate_name = getUniqueNameBasedOnCurrentTime('utmgmtcert')
def setUp(self): self.sms = ServiceManagementService( credentials.getSubscriptionId(), credentials.getManagementCertFile()) set_service_options(self.sms) self.affinity_group_name = getUniqueName('utaffgrp') self.hosted_service_name = None self.storage_account_name = None
def get_management_service(service, config): if service is ServiceManagementService: return ServiceManagementService(config['subscription_id'], config['key_file']) else: credential_service = ServicePrincipalCredentials( client_id=config['app_client_id'], secret=config['app_secret'], tenant=config['app_tenant']) return service(credentials=credential_service, subscription_id=config['subscription_id'])
def __build_management_service_instance(self): subscription_id = self.subscription_id() certificate_filename = self.certificate_filename() management_url = self.get_management_url() try: return ServiceManagementService(subscription_id, certificate_filename, management_url) except Exception as e: raise AzureServiceManagementError('%s: %s' % (type(e).__name__, format(e)))
def __init__(self): """Main execution path.""" # Inventory grouped by display group self.inventory = {} # Index of deployment name -> host self.index = {} self.host_metadata = {} # Cache setting defaults. # These can be overridden in settings (see `read_settings`). cache_dir = os.path.expanduser('~') self.cache_path_cache = os.path.join(cache_dir, '.ansible-azure.cache') self.cache_path_index = os.path.join(cache_dir, '.ansible-azure.index') self.cache_max_age = 0 # Read settings and parse CLI arguments self.read_settings() self.read_environment() self.parse_cli_args() # Initialize Azure ServiceManagementService self.sms = ServiceManagementService(self.subscription_id, self.cert_path) # Cache if self.args.refresh_cache: self.do_api_calls_update_cache() elif not self.is_cache_valid(): self.do_api_calls_update_cache() if self.args.list_images: data_to_print = self.json_format_dict(self.get_images(), True) elif self.args.list or self.args.host: # Display list of nodes for inventory if len(self.inventory) == 0: data = json.loads(self.get_inventory_from_cache()) else: data = self.inventory if self.args.host: data_to_print = self.get_host(self.args.host) else: # Add the `['_meta']['hostvars']` information. hostvars = {} if len(data) > 0: for host in set( [h for hosts in data.values() for h in hosts if h]): hostvars[host] = self.get_host(host, jsonify=False) data['_meta'] = {'hostvars': hostvars} # JSONify the data. data_to_print = self.json_format_dict(data, pretty=True) print(data_to_print)
def setUp(self): proxy_host = credentials.getProxyHost() proxy_port = credentials.getProxyPort() self.sms = ServiceManagementService( credentials.getSubscriptionId(), credentials.getManagementCertFile()) if proxy_host: self.sms.set_proxy(proxy_host, proxy_port) self.affinity_group_name = getUniqueNameBasedOnCurrentTime('utaffgrp') self.hosted_service_name = None self.storage_account_name = None
def main(argv): args = parse_args(argv) sms = ServiceManagementService(args.subscription_id, args.certificate_path) if args.command == 'delete-unused-disks': delete_unused_disks(sms, dry_run=args.dry_run, verbose=args.verbose) elif args.command == 'list-services': list_services(sms, glob=args.filter, verbose=args.verbose) elif args.command == 'delete-services': delete_services(sms, glob=args.filter, old_age=args.old_age, dry_run=args.dry_run, verbose=args.verbose) return 0
def from_boot_config(cls, boot_config): """A context manager for a AzureAccount. It writes the certificate to a temp file because the Azure client library requires it, then deletes the temp file when done. """ from azure.servicemanagement import ServiceManagementService config = get_config(boot_config) with temp_dir() as cert_dir: cert_file = os.path.join(cert_dir, 'azure.pem') open(cert_file, 'w').write(config['management-certificate']) service_client = ServiceManagementService( config['management-subscription-id'], cert_file) yield cls(service_client)
def delete_machine(self, instance_ids, deployment_name=None, cleanup_service=None, **kwargs): sms = ServiceManagementService(self.profile.username, self.cert_path) for inst in instance_ids: try: sms.delete_deployment(service_name=inst, deployment_name=deployment_name) if cleanup_service: sms.delete_hosted_service(service_name=inst) except Exception, ex: self.log.exception(ex) return self.FAIL
def __init__(self, subscription_id, pem_filepath): raise_if_none(subscription_id, 'subscription_id') raise_if_none(pem_filepath, 'pem_filepath') raise_if_path_not_exists(pem_filepath) self.name = "Azure" """Name of the cloud provider""" self.azure_subscription_id = subscription_id """Subscription ID in which we can consume Azure resources""" self.azure_pem_file = pem_filepath """Path to PEM file associated with the Azure subscription""" self.sms = ServiceManagementService( subscription_id=self.azure_subscription_id, cert_file=pem_filepath) """ServiceManagementService object for managing all services""" self.url = 'https://storage.googleapis.com/sweeper/configs/azure/configs.json' """URL to get config data"""
def _deleteVirtualMachines(self, service_name): """ Deletes the VMs in the given cloud service. """ if self._resource_exists(lambda: self.sms.get_deployment_by_name( service_name, service_name)) == False: logger.warn("Deployment %s not found: no VMs to delete.", service_name) else: logger.info("Attempting to delete deployment %s.", service_name) # Get set of role instances before we remove them role_instances = self._getRoleInstances(service_name) def update_request(request): """ A filter to intercept the HTTP request sent by the ServiceManagementService so we can take advantage of a newer feature ('comp=media') in the delete deployment API (see http://msdn.microsoft.com/en-us/library/windowsazure/ee460812.aspx) """ hdrs = [] for name, value in request.headers: if 'x-ms-version' == name: value = '2013-08-01' hdrs.append((name, value)) request.headers = hdrs request.path = request.path + '?comp=media' #pylint: disable=W0212 response = self.sms._filter(request) return response svc = ServiceManagementService(self.sms.subscription_id, self.sms.cert_file) #pylint: disable=W0212 svc._filter = update_request result = svc.delete_deployment(service_name, service_name) logger.info( "Deployment %s deletion in progress: waiting for delete_deployment operation.", service_name) self._wait_for_operation_success(result.request_id) logger.info( "Deployment %s deletion in progress: waiting for VM disks to be removed.", service_name) # Now wait for the disks to disappear for role_instance_name in role_instances.keys(): disk_name = "{0}.vhd".format(role_instance_name) self._wait_for_disk_deletion(disk_name) logger.info("Deployment %s deleted.", service_name)
def __get_sms_object(self, hackathon_id): """ Get ServiceManagementService object by Azure account which is related to hackathon_id :param hackathon_id: the id of hackathon :type hackathon_id: integer :return: ServiceManagementService object :rtype: class 'azure.servicemanagement.servicemanagementservice.ServiceManagementService' """ hackathon_azure_key = self.db.find_first_object_by(HackathonAzureKey, hackathon_id=hackathon_id) if hackathon_azure_key is None: self.log.error('Found no azure key with Hackathon:%d' % hackathon_id) return None sms = ServiceManagementService(hackathon_azure_key.azure_key.subscription_id, hackathon_azure_key.azure_key.pem_url, host=hackathon_azure_key.azure_key.management_host) return sms
def azure_add_endpoints(name, portConfigs): sms = ServiceManagementService(AZURE_SUBSCRIPTION_ID, AZURE_CERTIFICATE) role = sms.get_role(name, name, name) network_config = role.configuration_sets[0] for i, portConfig in enumerate(portConfigs): network_config.input_endpoints.input_endpoints.append( ConfigurationSetInputEndpoint( name=portConfig["service"], protocol=portConfig["protocol"], port=portConfig["port"], local_port=portConfig["local_port"], load_balanced_endpoint_set_name=None, enable_direct_server_return=True if portConfig["protocol"] == "udp" else False, idle_timeout_in_minutes=None if portConfig["protocol"] == "udp" else 4)) try: sms.update_role(name, name, name, network_config=network_config) except AzureHttpError as e: debug.warn("Exception opening ports for %s: %r" % (name, e))
def __init__(self, **azure_config): """ :param ServiceManagement azure_client: an instance of the azure serivce managment api client. :param String service_name: The name of the cloud service :param names of Azure volumes to identify cluster :returns: A ``BlockDeviceVolume``. """ self._instance_id = self.compute_instance_id() self._azure_service_client = ServiceManagementService( azure_config['subscription_id'], azure_config['management_certificate_path']) self._service_name = azure_config['service_name'] self._azure_storage_client = BlobService( azure_config['storage_account_name'], azure_config['storage_account_key']) self._storage_account_name = azure_config['storage_account_name'] self._disk_container_name = azure_config['disk_container_name'] if azure_config['debug']: to_file(sys.stdout)
def create_machine(self, name, region='West US', image=None, role_size='Small', min_count=1, max_count=1, media='storage_url_blob_cloudrunner', username='', password='', ssh_pub_key='', server=CR_SERVER, cleanup=None, **kwargs): self.log.info("Registering Azure machine [%s::%s] for [%s]" % (name, image, CR_SERVER)) try: sms = ServiceManagementService(self.profile.username, self._cert_path) server_config = LinuxConfigurationSet('myhostname', 'myuser', 'mypassword', True) media_link = "%s__%s" % (media, name) os_hd = OSVirtualHardDisk(image, media_link) res = sms.create_virtual_machine_deployment( service_name=name, deployment_name=name, deployment_slot='production', label=name, role_name=name, system_config=server_config, os_virtual_hard_disk=os_hd, role_size='Small') instance_ids = [] meta = {} if not res: return self.FAIL, [], {} meta['deployment_name'] = name meta['cleanup_service'] = cleanup in ['1', 'True', 'true'] return self.OK, instance_ids, meta except Exception, ex: self.log.exception(ex) return self.FAIL, [], {}
def __get_sms_object(self, hackathon_id): """ Get ServiceManagementService object by Azure account which is related to hackathon_id :param hackathon_id: the id of hackathon :type hackathon_id: integer :return: ServiceManagementService object :rtype: class 'azure.servicemanagement.servicemanagementservice.ServiceManagementService' """ hackathon_azure_keys = Hackathon.objects( id=hackathon_id).first().azure_keys if len(hackathon_azure_keys) == 0: self.log.error('Found no azure key with Hackathon:%d' % hackathon_id) return None hackathon_azure_key = hackathon_azure_keys[0] sms = ServiceManagementService( hackathon_azure_key.subscription_id, hackathon_azure_key.get_local_pem_url(), host=hackathon_azure_key.management_host) return sms
def __init__(self, subscription_id, cert_file): self.subscription_id = subscription_id self.cert_file = cert_file self.sms = ServiceManagementService(self.subscription_id, self.cert_file)
def __init__(self, config): self.config = config self.sms = ServiceManagementService(config.getAzureSubscriptionId(), config.getAzureCertificatePath()) self.sbms = ServiceBusManagementService( config.getAzureSubscriptionId(), config.getAzureCertificatePath())
from flask import Flask, redirect, render_template from azure.servicemanagement import ServiceManagementService from datetime import date subscription_id = '' cert_file = '' sms = ServiceManagementService(subscription_id, cert_file) service_name = '' deployment_name = '' role_name = '' post_shutdown_action = 'StoppedDeallocated' app = Flask(__name__) @app.route('/') def home(): deployments = sms.get_deployment_by_name(service_name, deployment_name) out0 = deployments.status if out0 == 'Running': return render_template("home.html", title='VM | Running', year=date.today().year) else: return render_template("home2.html", title='VM | Suspended', year=date.today().year) @app.route('/runit') def runit(): deployments = sms.get_deployment_by_name(service_name, deployment_name) out0 = deployments.status
import base64 from customparser import ConfigReader config_reader = ConfigReader('config.ini') config_params = config_reader.get_config_section_map("azure") # from http://stackoverflow.com/a/2257449 def name_generator(size=10, chars=string.ascii_lowercase + string.digits): return ''.join(random.choice(chars) for _ in range(size)) subscription_id = config_params["subscription_id"] certificate_path = config_params["mgmt_cert_path"] sms = ServiceManagementService(subscription_id, certificate_path) # Because the name has to be unique in Their cloud :/ hosted_service_name = name_generator() label = 'devOps test' desc = 'Service for basic nginx server' location = 'Central US' # image_list = sms.list_os_images() result = sms.create_hosted_service(hosted_service_name, label, desc, location) operation_result = sms.get_operation_status(result.request_id) storage_acc_name = name_generator() label = 'mystorageaccount' location = 'Central US'
def main(): ''' new version simulate a simple bash ''' config = __import__('config') subscription_id = get_certificate_from_publish_settings( publish_settings_path=config.publish_settings_path, path_to_write_certificate=config.path_to_write_certificate, ) cert_file = config.path_to_write_certificate sms = ServiceManagementService(subscription_id, cert_file) if len(sys.argv) < 2 : print "format should be python inspector.py <url of the vhd>" exit url = sys.argv[1] storage_name = url[8:url.find('.')] storage_account_key = sms.get_storage_account_keys(storage_name).storage_service_keys.primary.encode('ascii','ignore') nowpath = "/" def get_sentence(s) : st = s.find(' ') while st < len(s) and s[st] == ' ' : st += 1 ed = len(s) for i in range(st, len(s)) : if s[i] == ' ' and s[i-1] != '\\' : ed = i break while ed>0 and s[ed-1] == '/' : ed -= 1 return s[st:ed].replace("//", "/") global last_query_files_num while True : cmd = raw_input(nowpath+" $ ") if cmd.split(' ')[0] == "quit" : break elif cmd.split(' ')[0] == "ls" : old_main(url=url, account_key=storage_account_key, path=nowpath, ls=True) elif cmd.startswith("cd ") : sentence = get_sentence(cmd) if sentence != "" : if sentence == ".." : if nowpath != "/" : nowpath = nowpath[:nowpath[:-1].rfind('/')+1] elif sentence[0] == '/' : old_main(url=url, account_key=storage_account_key, path=sentence, ls=True) if last_query_files_num == 0 : print "no such directory" else : nowpath = sentence + "/" elif sentence != "" : old_main(url=url, account_key=storage_account_key, path=(nowpath+sentence), ls=True) if last_query_files_num == 0 : print "no such directory" else : nowpath += sentence + "/" elif cmd.startswith("download ") : sentence = get_sentence(cmd) tmp = sentence.rfind('/') if sentence != "" : old_main(url=url, account_key=storage_account_key, path=(nowpath+sentence[:tmp]), filename=sentence[(tmp+1):]) else : print "invalid command"
if not args.all and not args.cloudservice: print 'Cloudservice name missing' sys.exit(3) setup_logger(args.verbose) logger.debug('Converting publishsettings.') try: publishsettings = PublishSettings(args.psfile) except Exception, error: print 'Publishsettings file is not good. Error %s' % error sys.exit(3) if os.name != 'nt': pem_path = publishsettings.write_pem() logger.debug('Pem file saved to temp file {0}'.format(pem_path)) logger.debug('Azure sub id {0}'.format(publishsettings.sub_id)) management = ServiceManagementService( subscription_id=publishsettings.sub_id, cert_file=pem_path) else: logger.debug('Using cert to instantiate ServiceManagement.') if args.cert: management = ServiceManagementService(publishsettings.sub_id, cert_file=args.cert) else: print 'Cert is missing. Required on Windows' sys.exit(3) if args.key != 'status': logger.debug('Retrieving storage keys.') primary_key, _ = retrieve_keys(management, args.storageact) if not primary_key: if os.name != 'nt': os.unlink(pem_path)