def deployClusterTools(username, password, ipAddress): ssh = getSsh(ipAddress, username, password) executeSsh( ssh, 'curl -o /tmp/redis-stable.tar.gz http://download.redis.io/redis-stable.tar.gz', True) executeSsh(ssh, 'tar xzvf /tmp/redis-stable.tar.gz -C /tmp') executeSsh(ssh, 'apt-get -y install ruby', True) executeSsh(ssh, 'gem install redis')
def clusterAllNodes(username, password, ipAddress, ipAddresses): ssh = getSsh(ipAddress, username, password) nodes = [] for ip in ipAddresses: nodes.append('{0}:{1}'.format(ip, '7001')) nodes.append('{0}:{1}'.format(ip, '7002')) nodes.append('{0}:{1}'.format(ip, '7003')) nodes.append('{0}:{1}'.format(ip, '7004')) nodes.append('{0}:{1}'.format(ip, '7005')) nodes.append('{0}:{1}'.format(ip, '7006')) nodes.append('{0}:{1}'.format(ip, '7007')) nodes.append('{0}:{1}'.format(ip, '7008')) nodes.append('{0}:{1}'.format(ip, '7009')) executeSsh( ssh, 'echo yes | /tmp/redis-stable/src/redis-trib.rb create --replicas 2 {0}' .format(' '.join(nodes)))
def deploySystemDService(name, host, username, password, description, requires, after, start, stop): print('Connecting to remote server') ssh = getSsh(host, username, password) sftp = getSFtp(host, username, password) print('Connected') filename = createSystemdServiceFile(name, description, requires, after, start, stop) copyFileFromLocalToRemote(sftp, filename, os.path.join('/etc/systemd/system','{0}.service'.format(name)).replace('\\','/')) executeSsh(ssh, 'systemctl daemon-reload') executeSsh(ssh, 'systemctl start {0}.service'.format(name)) executeSsh(ssh, 'systemctl enable {0}.service'.format(name), ignoreErrors = True)
def deployDockerContainer(name, host, username, password, remoteBuildPath, args, dockerPath): print('Connecting to remote server') ssh = getSsh(host, username, password) sftp = getSFtp(host, username, password) print('Connected') copyFromLocalToRemote( ssh, sftp, dockerPath, os.path.join(remoteBuildPath, name).replace('\\', '/')) # executeSsh(ssh, 'docker build -t "{0}:tag" --no-cache {1}'.format(name, os.path.join(remoteBuildPath, name).replace('\\','/'))) executeSsh( ssh, 'docker build -t "{0}:tag" {1}'.format( name, os.path.join(remoteBuildPath, name).replace('\\', '/'))) executeSsh(ssh, 'docker kill {0}'.format(name), True) executeSsh(ssh, 'docker rm {0}'.format(name), True) executeSsh( ssh, 'docker run -d --name "{0}" {1} -t "{0}:tag"'.format(name, args)) print('Closing connection to remote server') ssh.close() sftp.close()
if __name__ == "__main__": parser = OptionParser() parser.add_option("-i", "--host", dest="host", help="Hostname or IP Address of server") parser.add_option("-u", "--username", dest="username", help="SSH username of server") parser.add_option("-p", "--password", dest="password", help="SSH password of server") parser.add_option("-c", "--command", dest="command", help="Ssh command to execute") (options, args) = parser.parse_args() host = options.host username = options.username password = options.password command = options.command ssh = getSsh(host, username, password) executeSsh(ssh, command)
def deployNodesToServer(username, password, ipAddress): ssh = getSsh(ipAddress, username, password) sftp = getSFtp(ipAddress, username, password) executeSsh(ssh, 'apt-get update') executeSsh(ssh, 'apt-get install -y redis-server', ignoreErrors=True) executeSsh(ssh, 'apt-get install -y tcl8.5', ignoreErrors=True) executeSsh(ssh, 'apt-get install -y build-essential', ignoreErrors=True) executeSsh(ssh, 'apt-get install -y wget', ignoreErrors=True) executeSsh(ssh, 'apt-get install -y make', ignoreErrors=True) executeSsh(ssh, 'wget http://download.redis.io/releases/redis-stable.tar.gz', ignoreErrors=True) executeSsh(ssh, 'tar xzf redis-stable.tar.gz') executeSsh(ssh, 'make -C /root/redis-stable', ignoreErrors=True) executeSsh(ssh, 'make -C /root/redis-stable install', ignoreErrors=True) for x in [ '7001', '7002', '7003', '7004', '7005', '7006', '7007', '7008', '7009' ]: filename = createRedisConfFile(x) copyFileFromLocalToRemote(sftp, filename, '/etc/redis/redis{0}.conf'.format(x))
dest="username", help="SSH username of server") parser.add_option("-p", "--password", dest="password", help="SSH password of server") (options, args) = parser.parse_args() username = options.username password = options.password ipAddresses = args[0].split(' ') for ipAddress in ipAddresses: deployNodesToServer(username, password, ipAddress) ssh = getSsh(ipAddress, username, password) for x in [ '7001', '7002', '7003', '7004', '7005', '7006', '7007', '7008', '7009' ]: # executeSsh(ssh, '(crontab -l 2>/dev/null; echo "@reboot /usr/bin/redis-server /etc/redis/redis{0}.conf") | crontab -'.format(x)) pass executeSsh(ssh, 'sudo reboot') print('Waiting for servers...') sleep(60) deployClusterTools(username, password, ipAddresses[0]) clusterAllNodes(username, password, ipAddresses[0], ipAddresses)