Class: Vagabund::Squatter::Provisioner

Inherits:
Object
  • Object
show all
Defined in:
lib/vagabund/squatter/provisioner.rb

Instance Method Summary collapse

Instance Method Details

#cleanupObject



14
15
16
17
# File 'lib/vagabund/squatter/provisioner.rb', line 14

def cleanup
  # remove the user?
  super
end

#configure(root_config) ⇒ Object



5
6
7
# File 'lib/vagabund/squatter/provisioner.rb', line 5

def configure(root_config)
  @root_config = root_config
end

#create_userObject



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
# File 'lib/vagabund/squatter/provisioner.rb', line 19

def create_user
  if config.user.create?
    if @machine.communicate.test "[ `getent passwd | grep -c '^#{config.user.username}:'` == 0 ]"
      @machine.ui.info "Creating user #{config.user.username}..."
      @machine.communicate.sudo config.user.to_s

      # Copy over the authorized_keys and known_hosts files being used currently for compatibility
      if !@machine.communicate.test "[ -d #{config.user.home}/.ssh ]"
        ssh_user_home = ''
        @machine.communicate.execute "echo $HOME" do |type,data|
          ssh_user_home = data.chomp if type == :stdout
        end
        
        @machine.communicate.sudo "mkdir -p #{config.user.home}/.ssh"
        
        if config.user.pubkeys.nil?
          # Copy the authorized_keys file if it doesn't exist and no public keys were provided
          if !@machine.communicate.test("[ -f #{config.user.home}/.ssh/authorized_keys ]") && @machine.communicate.test("[ -f #{ssh_user_home}/.ssh/authorized_keys ]")
            @machine.communicate.sudo "cp #{ssh_user_home}/.ssh/authorized_keys #{config.user.home}/.ssh/authorized_keys"
          end
        else
          # Add the public key(s) provided
          @machine.communicate.sudo "echo \"#{config.user.pubkeys}\" > #{config.user.home}/.ssh/authorized_keys"
        end

        unless config.user.ssh_conf_str.nil?
          @machine.communicate.sudo "echo \"#{config.user.ssh_conf_str}\" > #{config.user.home}/.ssh/config", verbose: true
        end
        
        if !@machine.communicate.test("[ -f #{config.user.home}/.ssh/known_hosts ]") && @machine.communicate.test("[ -f #{ssh_user_home}/.ssh/known_hosts ]")
          @machine.communicate.sudo "cp #{ssh_user_home}/.ssh/known_hosts #{config.user.home}/.ssh/known_hosts"
        end
        
        @machine.communicate.sudo "chown -R #{config.user.username} #{config.user.home}/.ssh"
        @machine.communicate.sudo "chgrp -R #{config.user.username} #{config.user.home}/.ssh"
      end

      # Add to sudoers
      if config.user.sudo
        @machine.communicate.sudo "echo \"#{config.user.username} ALL=(ALL) NOPASSWD:ALL\" > /etc/sudoers.d/#{config.user.username}"
        @machine.communicate.sudo "chmod 0440 /etc/sudoers.d/#{config.user.username}"
      end
    else
      @machine.ui.warn "User #{config.user.username} already exists"
    end
      
  end
rescue
  @machine.ui.error "Failed to create user #{config.user.username}"
  @machine.communicate.sudo "userdel -r #{config.user.username}" rescue nil
  @machine.communicate.sudo "rm -rf /etc/sudoers.d/#{config.user.username}"
end

#provisionObject



9
10
11
12
# File 'lib/vagabund/squatter/provisioner.rb', line 9

def provision
  create_user
  upload_files
end

#upload_filesObject



72
73
74
75
76
# File 'lib/vagabund/squatter/provisioner.rb', line 72

def upload_files
  config.files.each do |file|
    sync *expanded_paths(file)
  end
end