Module: Capistrano::Puppeteer::Bootstrap

Defined in:
lib/capistrano/puppeteer/bootstrap.rb

Class Method Summary collapse

Class Method Details

.extended(configuration) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/capistrano/puppeteer/bootstrap.rb', line 8

def self.extended(configuration)
  configuration.load do
    set(:bootstrap_domain) { abort "Please specify a domain, set :bootstrap_domain, 'inodes.org'" } unless exists? :bootstrap_domain
    set(:bootstrap_user)   { abort "Please specify a user, set :bootstrap_user, 'johnf'" } unless exists? :bootstrap_domain
    set(:puppet_path)      { abort "Please specify the path to puppet, set :puppet_path, '/srv/puppet'" } unless exists? :puppet_path
    set(:puppet_repo)      { abort "Please specify the path to puppet, set :puppet_repo, 'git@...'" } unless exists? :puppet_repo

    namespace :bootstrap do

      desc 'Create and bootstrap the server'
      task :create do
        puts "NOTE: Add host to puppet first and git push"
        puts
        sleep 5

        name = ENV['name'] or abort('please supply a name')
        ENV['fqdn'] ||= "#{name}.#{bootstrap_domain}"
        aws.create
        unless wait_for_ssh
          abort "Timed out waiting for SSH to come up on #{ENV['HOSTS']}"
        end
        bootstrap.go
      end

      def wait_for_ssh
        60.times do
          begin
            Timeout::timeout(1) do
              s = TCPSocket.new(ENV['HOSTS'], 22)
              s.close
              return true
            end
          rescue Timeout::Error, Errno::ECONNREFUSED, Errno::EHOSTUNREACH
          end
        end

        return false
      end

      desc 'Bootstrap the server with puppet'
      task :go do
        if exists? :ssh_key
          system "ssh-add #{ssh_key}"
        end
        hostname
        github
        upgrade
        puppet_setup
        unless ENV['skip_puppet']
          puppet_ubuntu if exists?(:cloud_provider) && cloud_provider == 'AWS'
          puppet.go
        end
      end

      task :hostname do
        set :user, 'ubuntu' if exists?(:cloud_provider) && cloud_provider == 'AWS'
        fqdn = ENV['fqdn'] || Capistrano::CLI.ui.ask('What is the full fqdn for the host')
        hostname = fqdn.split('.')[0]

        run "#{sudo} sudo sed -i -e '/127.0.0.1/a127.0.1.1 #{fqdn} #{hostname}' /etc/hosts"
        run "echo #{hostname} | #{sudo} tee /etc/hostname > /dev/null"
        run "#{sudo} sed -itmp -e 's/\\(domain\\|search\\).*/\\1 #{bootstrap_domain}/' /etc/resolv.conf"
        run "#{sudo} service hostname start"
      end

      task :github do
        set :user, 'ubuntu' if exists?(:cloud_provider) && cloud_provider == 'AWS'
        run "mkdir -p .ssh"
        run "echo 'github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==' >> .ssh/known_hosts"
      end

      task :upgrade do
        set :user, 'ubuntu' if exists?(:cloud_provider) && cloud_provider == 'AWS'

        run "#{sudo} apt-get -y update"
        run "DEBIAN_FRONTEND=noninteractive #{sudo} -E apt-get -y dist-upgrade"
      end

      def remote_file_exists?(full_path)
        'true' ==  capture("if [ -e #{full_path} ]; then echo 'true'; fi").strip
      end

      task :puppet_setup do
        set :user, 'ubuntu' if exists?(:cloud_provider) && cloud_provider == 'AWS'

        release = capture 'lsb_release  --codename --short | tr -d "\n"'
        fail "unable to determine distro release" if release.empty?

        # add puppetlabs apt repos
        unless remote_file_exists? puppet_path
          filename = '/etc/apt/sources.list.d/puppetlabs.list'
          run "echo 'deb http://apt.puppetlabs.com/ #{release} main' | #{sudo} tee #{filename}"
        end
        run "#{sudo} apt-key adv --keyserver keyserver.ubuntu.com --recv 4BD6EC30"
        run "#{sudo} apt-get -yq update"
        run "#{sudo} apt-get install -y puppet libaugeas-ruby git"

        unless remote_file_exists? puppet_path
          run "git clone #{puppet_repo} /tmp/puppet"
          run "#{sudo} mv /tmp/puppet #{puppet_path}"
        end

        run "#{sudo} mkdir -p /home/#{bootstrap_user}/.ssh"
        run "#{sudo} echo 'github.com ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==' | #{sudo} tee /home/#{bootstrap_user}/.ssh/known_hosts"
      end

      task :puppet_ubuntu do
        set :user, 'ubuntu'

        run 'cd /srv/puppet && git pull'

        puppet.go
      end

      task :reboot do
        set :user, ENV['USER']

        run "#{sudo} reboot"
      end

    end

  end
end