Class: Dreamback::Initializer

Inherits:
Object
  • Object
show all
Defined in:
lib/dreamback/initializer.rb

Constant Summary collapse

SETTINGS_LOCATION =
File.expand_path("~/.dreamback")
DEFAULT_SETTINGS =
{
  :backup_server => "",
  :backup_server_user => "",
  :dreamhost_users => [ {:user => "", :server => ""} ]
}

Class Method Summary collapse

Class Method Details

.setup(direct = false) ⇒ Object

Walks a user through the initial setup process



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
# File 'lib/dreamback/initializer.rb', line 29

def self.setup(direct = false)
  # Check to see if settings exist
  load_settings(SETTINGS_LOCATION)

  # Ask user questions if no settings file
  if @settings.nil? || @settings.empty?
    create_new_settings
    save_settings(SETTINGS_LOCATION)
  else
    if direct
      say(bold("You have already setup Dreamback."))

      choose("From here you can:  \n") do |menu|
        menu.prompt = "Select an option:  "

        menu.choice("Run setup again") {
          create_new_settings
          save_settings(SETTINGS_LOCATION)
        }

        menu.choice("Run a backup right now") {
          Dreamback::Backup.start
        }
      end
    end
  end

  # Create ssh keys if they don't exist
  ssh_keys_exist = File.exists?(File.expand_path("~/.ssh/id_dsa"))
  create_ssh_keys unless ssh_keys_exist

  # Copy ssh keys to backup server
  unless @settings[:copied_backup_server_ssh_keys]
    say(bold("Copying the ssh key to your backup server, type in your password if prompted for #{@settings[:backup_server_user]}@#{@settings[:backup_server]}"))
    overwrite_keys = agree(bold("WARNING: ") + "This will overwrite existing ssh keys on your backup user account. Proceed? [y/n]: ")
    if overwrite_keys
      sftp_password = ask("Password for #{@settings[:backup_server_user]}@#{@settings[:backup_server]}: ") { |q| q.echo = "*" }
      sftp_ssh_key_upload(sftp_password)
    else
      say("You will need to add the ssh key yourself to automate backups")
    end
    @settings[:copied_backup_server_ssh_keys] = true
  end

  # Copy ssh keys to dreamhost servers
  unless @settings[:copied_dreamhost_users_ssh_keys]
    say(bold("Copying the ssh key to the dreamhost accounts you want to back up"))
    @settings[:dreamhost_users].each do |dreamhost|
      say(bold("Type in password if prompted for #{dreamhost[:user]}@#{dreamhost[:server]}"))
      `ssh-copy-id -i #{dreamhost[:user]}@#{dreamhost[:server]}`
    end
    @settings[:copied_dreamhost_users_ssh_keys] = true
  end

  # Setup a cron job if the user would like to
  unless @settings[:cron_setup_completed]
    setup_cron = agree(bold("Would you like to add a cron job to automatically run the backup? [y/n]: "))
    if setup_cron
      crontab_email = ask("Dreamhost requires an email address to send crontab output to, please provide one: ") { |q| q.validate = /\b[A-Za-z0-9._%-\+]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}\b/ }

      # Set environment if these paths exist
      gems = File.expand_path("/etc/profile")
      if File.exists?(gems)
        cron_profile = ". /etc/profile &&"
      end

      ct = File.open(File.expand_path("~/.dreamback_crontab"), "w+")
      ct << "MAILTO=#{crontab_email}\n"
      ct << "0 1 * * * #{cron_profile} dreamback backup"
      ct.close
      `crontab #{ct.path}`
      File.delete(ct.path)
      say("Cron job added. Backups will run at 1:00am Pacific every day.")
    end
    @settings[:cron_setup_completed] = true
  end

  save_settings(SETTINGS_LOCATION)

  # Set global settings
  Dreamback.settings = @settings
end