Skip to content

Commit

Permalink
Merge pull request saltstack#77 from techhat/develop
Browse files Browse the repository at this point in the history
Add event-reactive initialization for saltstack#59
  • Loading branch information
thatch45 committed Oct 4, 2012
2 parents 744d9ff + c7b7623 commit 22586b3
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 6 deletions.
4 changes: 3 additions & 1 deletion saltcloud/clouds/aws.py
Expand Up @@ -221,7 +221,9 @@ def create(vm_):
key_filename=__opts__['AWS.private_key'],
deploy_command=deploy_command,
tty=True,
script=deploy_script.script)
script=deploy_script.script,
name=vm_['name'],
sock_dir=__opts__['sock_dir'])
if deployed:
print('Salt installed on {0}'.format(vm_['name']))
else:
Expand Down
4 changes: 3 additions & 1 deletion saltcloud/clouds/gogrid.py
Expand Up @@ -92,7 +92,9 @@ def create(vm_):
host=data.public_ips[0],
username='root',
password=data.extra['password'],
script=deploy_script.script)
script=deploy_script.script,
name=vm_['name'],
sock_dir=__opts__['sock_dir'])
if deployed:
print('Salt installed on {0}'.format(vm_['name']))
else:
Expand Down
4 changes: 3 additions & 1 deletion saltcloud/clouds/linode.py
Expand Up @@ -114,7 +114,9 @@ def create(vm_):
host=data.public_ips[0],
username='root',
password=__opts__['LINODE.password'],
script=deploy_script.script)
script=deploy_script.script,
name=vm_['name'],
sock_dir=__opts__['sock_dir'])
if deployed:
print('Salt installed on {0}'.format(vm_['name']))
else:
Expand Down
4 changes: 3 additions & 1 deletion saltcloud/clouds/rackspace.py
Expand Up @@ -90,7 +90,9 @@ def create(vm_):
host=data.public_ips[0],
username='root',
password=data.extra['password'],
script=deploy_script.script)
script=deploy_script.script,
name=vm_['name'],
sock_dir=__opts__['sock_dir'])
if deployed:
print('Salt installed on {0}'.format(vm_['name']))
else:
Expand Down
40 changes: 38 additions & 2 deletions saltcloud/utils/__init__.py
Expand Up @@ -11,6 +11,9 @@
import time
import paramiko
import subprocess
import salt.utils.event
import multiprocessing
import time

# Import salt libs
import salt.crypt
Expand Down Expand Up @@ -178,12 +181,16 @@ def wait_for_passwd(host, port=22, timeout=900, username='root',

def deploy_script(host, port=22, timeout=900, username='root',
password=None, key_filename=None, script=None,
deploy_command='bash /tmp/deploy.sh', tty=None):
deploy_command='bash /tmp/deploy.sh', tty=None,
name=None, pub_key=None, sock_dir=None):
'''
Copy a deploy script to a remote server, execute it, and remove it
'''
starttime = time.mktime(time.localtime())
if wait_for_ssh(host=host, port=port, timeout=timeout):
if wait_for_passwd(host, port=port, username=username, password=password, key_filename=key_filename, timeout=timeout):
newtimeout = timeout - (time.mktime(time.localtime()) - starttime)
if wait_for_passwd(host, port=port, username=username, password=password, key_filename=key_filename, timeout=newtimeout):
newtimeout = timeout - (time.mktime(time.localtime()) - starttime)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
kwargs = {'hostname': host,
Expand All @@ -208,6 +215,15 @@ def deploy_script(host, port=22, timeout=900, username='root',
sftp.put(tmppath, '/tmp/deploy.sh')
os.remove(tmppath)
ssh.exec_command('chmod +x /tmp/deploy.sh')

newtimeout = timeout - (time.mktime(time.localtime()) - starttime)
queue = multiprocessing.Queue()
process = multiprocessing.Process(
target=lambda: check_auth(name=name, pub_key=pub_key, sock_dir=sock_dir,
timeout=newtimeout, queue=queue),
)
process.start()

if tty:
# Tried this with paramiko's invoke_shell(), and got tired of
# fighting with it
Expand All @@ -223,5 +239,25 @@ def deploy_script(host, port=22, timeout=900, username='root',
for line in stdout:
sys.stdout.write(line)
ssh.exec_command('rm /tmp/deploy.sh')
queuereturn = queue.get()
process.join()
return True
return False

def check_auth(name, pub_key=None, sock_dir=None, queue=None, timeout=300):
event = salt.utils.event.SaltEvent(
'master',
sock_dir,
)
starttime = time.mktime(time.localtime())
newtimeout = timeout
while newtimeout > 0:
newtimeout = timeout - (time.mktime(time.localtime()) - starttime)
ret = event.get_event(full=True)
if ret is None:
continue
if ret['tag'] == 'auth' and ret['data']['id'] == name:
queue.put(True)
newtimeout = 0
else:
queue.put(False)

0 comments on commit 22586b3

Please sign in to comment.