Class: VagrantPlugins::DigitalOcean::Actions::SetupUser

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-digitalocean/actions/setup_user.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ SetupUser

Returns a new instance of SetupUser.



5
6
7
8
# File 'lib/vagrant-digitalocean/actions/setup_user.rb', line 5

def initialize(app, env)
  @app, @env = app, env
  @translator = Helpers::Translator.new("actions.setup_user")
end

Instance Method Details

#call(env) ⇒ Object



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
# File 'lib/vagrant-digitalocean/actions/setup_user.rb', line 10

def call(env)
  # create the user, set the password to username, add to sudoers
  # NOTE assumes group with username is created with useradd
  env[:ui].info @translator.t("create", :user => user)
  env[:machine].communicate.execute(<<-BASH)
    if ! (grep #{user} /etc/passwd); then
      useradd -m -s /bin/bash #{user};
      echo -e "#{user}\n#{user}" | (passwd #{user});
    fi
  BASH

  env[:ui].info @translator.t("sudo", :user => user)
  env[:machine].communicate.execute(<<-BASH)
    if ! (grep #{user} /etc/sudoers); then
      echo "#{user} ALL=(ALL:ALL) ALL" >> /etc/sudoers;
    fi
  BASH

  # create the .ssh directory in the users home
  env[:machine].communicate.execute("su #{user} -c 'mkdir -p ~/.ssh'")

  env[:ui].info @translator.t("key")
  # add the specified key to the authorized keys file
  env[:machine].communicate.execute(<<-BASH)
    if ! grep '#{pub_key}' /home/#{user}/.ssh/authorized_keys; then
      echo '#{pub_key}' >> /home/#{user}/.ssh/authorized_keys;
    fi
  BASH

  env[:machine_state] ||= {}
  env[:machine_state][:user] = user

  @app.call(env)
end