|
| 1 | +from fabric.api import * |
| 2 | +import getpass |
| 3 | +import sys |
| 4 | + |
| 5 | +# Enforce sysadmin params |
| 6 | +env.user = "bhaisaab" |
| 7 | +env.port = "1009" |
| 8 | + |
| 9 | +# Forward local agent |
| 10 | +env.forward_agent = True |
| 11 | + |
| 12 | +# Move to a reusable map/hash module |
| 13 | +env.roledefs = { |
| 14 | + "baagi": [""], |
| 15 | + } |
| 16 | + |
| 17 | +# Add role to capture all nodes |
| 18 | +all_servers = [] |
| 19 | +for key in env.roledefs: |
| 20 | + all_servers = all_servers + env.roledefs[key] |
| 21 | + |
| 22 | +env.roledefs["all"] = all_servers |
| 23 | + |
| 24 | +if len(env.hosts) != 0: |
| 25 | + for key in env.roledefs.keys(): |
| 26 | + env.roledefs[key] = [] |
| 27 | + |
| 28 | + |
| 29 | +print """ |
| 30 | + _____ __ __ |
| 31 | + / _ \ __ ___/ |_ ____ _____ _____ _/ |_ ___________ |
| 32 | + / /_\ \| | \ __\/ _ \ / \\\\__ \\\\ __\/ _ \_ __ \\ |
| 33 | +/ | \ | /| | ( <_> ) Y Y \/ __ \| | ( <_> ) | \/ |
| 34 | +\____|__ /____/ |__| \____/|__|_| (____ /__| \____/|__| |
| 35 | + \/ \/ \/ |
| 36 | +""" |
| 37 | + |
| 38 | +print "Tasks:", env['tasks'] |
| 39 | +print "Roles:", env['roles'] |
| 40 | +print "Hosts:", env['hosts'] |
| 41 | +print "SSH Port:", env['port'] |
| 42 | + |
| 43 | + |
| 44 | +if "init" not in sys.argv and not env.password: |
| 45 | + env.password = getpass.getpass("Enter OTP for sudo ops: ") |
| 46 | + |
| 47 | + |
| 48 | +def info(): |
| 49 | + run("uname -a") |
| 50 | + run("lsb_release -a") |
| 51 | + run("uptime") |
| 52 | + run("last | head -5") |
| 53 | + run("hostname && hostname -f") |
| 54 | + |
| 55 | + |
| 56 | +def upgrade(): |
| 57 | + sudo("apt-get update && apt-get upgrade -V") |
| 58 | + |
| 59 | + |
| 60 | +def noop(): |
| 61 | + sudo("cd /etc/puppet/ && git clean -fd && git checkout -- /etc/puppet/ && git pull --rebase origin master") |
| 62 | + sudo("puppet apply --modulepath /etc/puppet/modules --noop /etc/puppet/manifests/site.pp --templatedir /etc/puppet/templates/") |
| 63 | + |
| 64 | + |
| 65 | +def deploy(): |
| 66 | + """ |
| 67 | + Runs puppet apply |
| 68 | + """ |
| 69 | + sudo("cd /etc/puppet/ && git clean -fd && git checkout -- /etc/puppet/ && git pull --rebase origin master") |
| 70 | + sudo("puppet apply --modulepath /etc/puppet/modules /etc/puppet/manifests/site.pp --templatedir /etc/puppet/templates/ --debug") |
| 71 | + |
| 72 | + |
| 73 | +def reboot(): |
| 74 | + sudo("reboot") |
| 75 | + |
| 76 | + |
| 77 | +def init(): |
| 78 | + """ |
| 79 | + Assumed that root user will setup initial environment before admin takes control |
| 80 | + """ |
| 81 | + if len(env.hosts) > 1: |
| 82 | + print "WARNING: You're initializing more than one host in one go!" |
| 83 | + |
| 84 | + env.user = "root" |
| 85 | + env.port = "22" |
| 86 | + |
| 87 | + # host info |
| 88 | + info() |
| 89 | + |
| 90 | + # basic package management |
| 91 | + run("apt-get update && apt-get upgrade -y") |
| 92 | + run("apt-get purge -y exim* mutt procmail bind9 apache2* php5* mysql* mailagent") |
| 93 | + run("apt-get install --no-install-recommends -y vim htop sudo openssh-client ssh wget gcc build-essential python-pip git tig") |
| 94 | + |
| 95 | + # append local public key to authorized_keys |
| 96 | + put("~/.ssh/id_rsa.pub", "/tmp") |
| 97 | + run("mkdir -p /root/.ssh && cat /tmp/id_rsa.pub >> /root/.ssh/authorized_keys") |
| 98 | + |
| 99 | + # fix ulimits |
| 100 | + run("echo -e '* \t soft \t nofile \t 64000' >> /etc/security/limits.conf") |
| 101 | + run("echo -e '* \t hard \t nofile \t 128000' >> /etc/security/limits.conf") |
| 102 | + run("echo -e 'root \t soft \t nofile \t 64000' >> /etc/security/limits.conf") |
| 103 | + run("echo -e 'root \t hard \t nofile \t 128000' >> /etc/security/limits.conf") |
| 104 | + |
| 105 | + # install puppet based on Debian codename |
| 106 | + run("if [ `lsb_release --codename | grep wheezy | wc -l` -eq 1 ]; then cd /tmp && wget http://apt.puppetlabs.com/puppetlabs-release-wheezy.deb && dpkg -i puppetlabs-release-wheezy.deb; else cd /tmp && wget http://apt.puppetlabs.com/puppetlabs-release-squeeze.deb && dpkg -i puppetlabs-release-squeeze.deb; fi") |
| 107 | + |
| 108 | + # install puppet and git, clone repo |
| 109 | + run("apt-get update && apt-get install puppet -y --no-install-recommends") |
| 110 | + run("cd /etc && rm -fr puppet && git clone https://github.com/baagi/devops.git puppet") |
| 111 | + |
| 112 | + # first deploy |
| 113 | + deploy() |
0 commit comments