Skip to content

Commit

Permalink
login_to
Browse files Browse the repository at this point in the history
  • Loading branch information
lee212 committed Oct 2, 2016
1 parent 8d23f6a commit 5846215
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 28 deletions.
63 changes: 38 additions & 25 deletions simpleazure/simpleazure.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,25 +185,21 @@ def create_vm(self, name=None, location=None):

self.linux_user_passwd = generate_password(16)
os_hd = OSVirtualHardDisk(self.image_name, self.media_link)
linux_config = LinuxConfigurationSet(self.get_name(), self.linux_user_id,
self.linux_user_passwd, self.ssh_key_is_on)
linux_config = LinuxConfigurationSet(self.get_name(),
self.linux_user_id, self.linux_user_passwd, self.ssh_key_is_on)

self.set_ssh_keys(linux_config)
self.set_network()
self.set_service_certs()
# can't find certificate right away.
#sleep(5)
sleep(5)

result = \
self.sms.create_virtual_machine_deployment(service_name=self.get_name(), \
deployment_name=self.get_name(), \
deployment_slot='production',\
label=self.get_name(), \
role_name=self.get_name(), \
system_config=linux_config, \
os_virtual_hard_disk=os_hd, \
network_config=self.network,\
role_size=self.get_role_size())
self.sms.create_virtual_machine_deployment(service_name=self.get_name(),\
deployment_name=self.get_name(), deployment_slot='production',\
label=self.get_name(), role_name=self.get_name(), \
system_config=linux_config, os_virtual_hard_disk=os_hd, \
network_config=self.network, role_size=self.get_role_size())

self.result = result
return result
Expand All @@ -224,7 +220,8 @@ def connect_service(self, refresh=False):
if not hasattr(self, 'cert'):
self.get_creds()
if not self.sms or refresh:
self.sms = ServiceManagementService(self.subscription_id, self.certificate_path)
self.sms = ServiceManagementService(self.subscription_id,
self.certificate_path)

def create_cloud_service(self, name=None, location=None):
"""Create a cloud (hosted) service via create_hosted_service()
Expand All @@ -239,7 +236,8 @@ def create_cloud_service(self, name=None, location=None):
name = self.get_name()
if not location:
location = self.location
self.sms.create_hosted_service(service_name=name, label=name, location=location)
self.sms.create_hosted_service(service_name=name, label=name,
location=location)

def use_ssh_key(self, yn=False):
"""Enable SSH Key to connect"""
Expand All @@ -259,7 +257,8 @@ def set_ssh_keys(self, config):
# fingerprint captured by 'openssl x509 -in myCert.pem -fingerprint
# -noout|cut -d"=" -f2|sed 's/://g'> thumbprint'
# (Sample output) C453D10B808245E0730CD023E88C5EB8A785ED6B
self.thumbprint = open(self.thumbprint_path, 'r').readline().split('\n')[0]
self.thumbprint = open(self.thumbprint_path,
'r').readline().split('\n')[0]
publickey = PublicKey(self.thumbprint, self.public_key_path)
# KeyPair is a SSH kay pair both a public and a private key to be stored
# on the virtual machine.
Expand Down Expand Up @@ -298,7 +297,8 @@ def set_network(self):
"""
network = ConfigurationSet()
network.configuration_set_type = 'NetworkConfiguration'
network.input_endpoints.input_endpoints.append(ConfigurationSetInputEndpoint('ssh', 'tcp', '22', '22'))
network.input_endpoints.input_endpoints.append(ConfigurationSetInputEndpoint('ssh',
'tcp', '22', '22'))
self.network = network

def set_service_certs(self):
Expand Down Expand Up @@ -407,7 +407,8 @@ def list_deployments(self):
def delete_vm(self, name=None):
"""Delete vm instance"""

res = self.sms.delete_deployment(name or self.get_name(), name or self.get_name(), delete_vhd=True)
res = self.sms.delete_deployment(name or self.get_name(), name or
self.get_name(), delete_vhd=True)
return res

def set_image(self, name=None, image=None, refresh=False):
Expand Down Expand Up @@ -500,8 +501,8 @@ def get_last_storage_account(self, refresh=False):
try:
return storage_account
except:
self.create_storage_account()
storage_account = self.get_name()
storage_account = self.create_storage_account()
#storage_account = self.get_name()
return storage_account

def create_storage_account(self):
Expand All @@ -511,6 +512,8 @@ def create_storage_account(self):
self.sms.create_storage_account(service_name=name,
description=description, label=label,
location=self.get_location())
self.storage_name = name
return name

def get_account_from_link(self, url):
"""Return hostname from a link.
Expand Down Expand Up @@ -548,9 +551,10 @@ def create_cluster(self, num=None, option=None):
results = {}
# It is supposed to use multi-processing instead of for loop
for cnt in range(cluster_count):
self.set_name(self.cluster_name_prefix + str(cnt) + "-" + self.get_random())
self.set_name(self.cluster_name_prefix + str(cnt) + "-" +
self.get_random())
result = self.create_vm(self.get_name())
sleep(10)
sleep(15)
#results.append(result)
results[self.get_name()] = result
if cnt == 0:
Expand All @@ -562,7 +566,7 @@ def create_cluster(self, num=None, option=None):
self.results = results
return results

def login_to(self, name=None):
def login_to(self, name=None, login_id=None, passwd=None, ssh_key=None):
"""SSH to a virtual machine
:param name: (optional) the hostname of a virtual machine
Expand All @@ -576,7 +580,14 @@ def login_to(self, name=None):
hostname = config.get_azure_domain(name)

sshmaster = ssh.SSH()
sshmaster.setup(host=hostname, pkey=self.private_key_path)
self.sshmaster = sshmaster
if ssh_key:
sshmaster.setup(host_string = hostname, key_filename = ssh_key or
self.private_key_path)
else:
sshmaster.setup(host_string = hostname, user = login_id or
self.linux_user_id, password = passwd or
self.linux_user_passwd)
sshmaster.shell()

def get_username(self):
Expand All @@ -593,7 +604,8 @@ def purge_all(self):
# Delete hosted services
hosted_services = self.sms.list_hosted_services()
for i in hosted_services:
svc_props = self.sms.get_hosted_service_properties(i.service_name, True)
svc_props = self.sms.get_hosted_service_properties(i.service_name,
True)
for j in svc_props.deployments:
self.delete_vm(j.name) #i.service_name)
# log("{0} (vm) deletion requested".format(i.service_name))
Expand Down Expand Up @@ -644,7 +656,8 @@ def get_all_items(self):
hosted_services = self.sms.list_hosted_services()
all_items['hosted_services']['count'] = len(hosted_services)
for i in hosted_services:
svc_props = self.sms.get_hosted_service_properties(i.service_name, True)
svc_props = self.sms.get_hosted_service_properties(i.service_name,
True)
all_items['deployments']['count'] += len(svc_props.deployments)
for j in svc_props.deployments:
all_items['deployments']['names'].append(j.name)
Expand Down
9 changes: 6 additions & 3 deletions simpleazure/ssh.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from fabric.api import run, env

class SSH:
def setup(self, host=None, pkey=None):
env.host_string = host
env.key_filename = pkey
# def setup(self, host=None, pkey=None):
def setup(self, **kwargs):
for k,v in kwargs.iteritems():
exec('env.' + k + '="' + v + '"')
#env.host_string = host
#env.key_filename = pkey

def shell(self):
run('bash')
Expand Down

0 comments on commit 5846215

Please sign in to comment.