def deploy(c, restart=False, sync=False): "push code to a server (configure your own here)" c.run("git push --all") conn = Connection("trotsky") with conn.cd("src/pyborg-1up"): conn.run("git fetch") conn.run("git stash") conn.run("git pull") conn.run("git stash pop") if sync: conn.run("~/.local/bin/pipenv sync") # they all use the same pipenv managed virtualenv if restart: for unit in ["pyborg_discord", "pyborg_http", "pyborg_twitter", "pyborg_mastodon"]: conn.sudo("systemctl restart {}".format(unit), pty=True) print("Deploy Completed.")
def ssh_run(servername, cmd, sudo): try: user = os.environ['SSH_USER'] passw = os.environ['SSH_PASS'] sudotf = sudo command = cmd sname = servername print('user: %s passwd: %s servername: %s' % (user, passw, sname)) config = Config(overrides={'sudo': {'password': passw}}) c = Connection(host=sname, user=user, port=22, config=config, connect_kwargs={"password": passw}) if sudotf: result = c.sudo(command, pty=True, hide='stderr') if result.exited == 0: print("In SUDO") print(result.ok) print(result.stdout.strip()) return result.exited else: result = c.run(SSH_COMMAND, pty=True, hide='stderr') if result.exited == 0: print("In NOSUDO") print(result.ok) print(result.stdout.strip()) return result.exited except Exception as e: print("ssh_run Exception Caught: %s" % e) return "Exception Caught Clearing Certificate"
class FabricWrapper: def __init__( self, host, user=None, password=None, key_filename=None, key_passphrase=None, port=22, sudo=False ): self.sudo = sudo if not user and not password and not key_filename: # Get details from ~/.ssh/config self.connection = Connection(host) elif key_filename and not password: self.connection = Connection( host=host, user=user, port=port, connect_kwargs={ 'key_filename': key_filename, 'passphrase': key_passphrase } ) elif not key_filename and password: self.connection = Connection( host=host, user=user, port=port, connect_kwargs={ 'password': password } ) elif key_filename and password: self.connection = Connection( host=host, user=user, port=port, connect_kwargs={ 'key_filename': key_filename, 'passphrase': key_passphrase if key_passphrase else password } ) else: logging.error( crayons.red(f'You need to provide either a private key_filename or password to connect to {host} with user: {user}') ) self.connection = None def execute(self, command, **kwargs): if not self.connection: logging.error(crayons.red('No connection object instantiated.')) return None return self.connection.sudo(command, **kwargs) if self.sudo else self.connection.run(command, **kwargs)
from fabric2 import Connection, Config config = Config(overrides={'sudo': {'password': '******'}}) c = Connection(host='server', user='******', port=22, config=config, connect_kwargs={"password": "******"}) result = c.sudo('whoami', pty=True, hide='stdout') print(result.exited) print(result.ok) #print(result.stdout.strip())