Class: Ssync::Setup

Inherits:
Object
  • Object
show all
Extended by:
Helpers
Defined in:
lib/ssync/setup.rb

Class Method Summary collapse

Methods included from Helpers

aquire_lock!, ask, config_exists?, config_path, create_homedir!, default_config_path, display, display_error, exit_with_error!, lock_path, read_config, read_default_config, ssync_filename, ssync_homedir, write_config!, write_default_config!

Class Method Details

.aws_credentials_is_valid?(config = read_config) ⇒ Boolean

Returns:

  • (Boolean)


69
70
71
72
73
74
75
76
77
78
79
# File 'lib/ssync/setup.rb', line 69

def aws_credentials_is_valid?(config = read_config)
  AWS::S3::Base.establish_connection!(:access_key_id => config[:aws_access_key], :secret_access_key => config[:aws_secret_key])
  begin
    # AWS::S3 don't try to connect at all until you ask it for something.
    AWS::S3::Service.buckets
  rescue AWS::S3::InvalidAccessKeyId => e
    false
  else
    true
  end
end

.bucket_empty?(config = read_config) ⇒ Boolean

Returns:

  • (Boolean)


87
88
89
# File 'lib/ssync/setup.rb', line 87

def bucket_empty?(config = read_config)
  AWS::S3::Bucket.find(config[:aws_dest_bucket]).empty?
end

.bucket_exists?(config = read_config) ⇒ Boolean

Returns:

  • (Boolean)


81
82
83
84
85
# File 'lib/ssync/setup.rb', line 81

def bucket_exists?(config = read_config)
  AWS::S3::Bucket.find(config[:aws_dest_bucket])
rescue AWS::S3::NoSuchBucket => e
  false
end

.configObject



14
15
16
# File 'lib/ssync/setup.rb', line 14

def config
  @config ||= read_config
end

.config=(config) ⇒ Object



18
19
20
# File 'lib/ssync/setup.rb', line 18

def config=(config)
  @config = config
end

.create_bucket(config = read_config) ⇒ Object



91
92
93
# File 'lib/ssync/setup.rb', line 91

def create_bucket(config = read_config)
  AWS::S3::Bucket.create(config[:aws_dest_bucket])
end

.default_configObject



6
7
8
# File 'lib/ssync/setup.rb', line 6

def default_config
  @default_config ||= read_default_config
end

.default_config=(config) ⇒ Object



10
11
12
# File 'lib/ssync/setup.rb', line 10

def default_config=(config)
  @default_config = config
end

.local_file_path_exists?(config = read_config) ⇒ Boolean

Returns:

  • (Boolean)


95
96
97
# File 'lib/ssync/setup.rb', line 95

def local_file_path_exists?(config = read_config)
  File.exist?(config[:local_file_path])
end

.run!Object



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
# File 'lib/ssync/setup.rb', line 22

def run!
  display "Welcome to Ssync! You will now be asked for a few questions."

  config[:aws_access_key] = ask config[:aws_access_key], "What is the AWS Access Key ID?"
  config[:aws_secret_key] = ask config[:aws_secret_key], "What is the AWS Secret Access Key?"

  display "Please wait while Ssync is connecting to AWS ..."

  if aws_credentials_is_valid?(config)
    display "Successfully connected to AWS."

    default_config[:last_used_bucket] = config[:aws_dest_bucket] = ask(config[:aws_dest_bucket], "Which bucket would you like to put your backups in? Ssync will create the bucket for you if it doesn't exist.")

    if bucket_exists?(config)
      if bucket_empty?(config)
        display "The bucket exists and is empty, great!"
      else
        e! "The bucket exists but is not empty, we cannot sync to a bucket that is not empty!"
      end
    else
      display "The bucket doesn't exist, creating it now ..."
      create_bucket(config)
      display "The bucket has been created."
    end
  else
    e! "Ssync wasn't able to connect to AWS, please check the credentials you supplied are correct."
  end

  require "pathname"
  config[:local_file_path] = ask config[:local_file_path], "What is the path you would like to backup? (i.e. '/var/www')."
  config[:local_file_path] = Pathname.new(config[:local_file_path]).realpath.to_s

  if local_file_path_exists?(config)
    display "The path is set to '#{config[:local_file_path]}'."
  else
    e! "The path you specified does not exist!"
  end

  config[:find_options] = ask config[:find_options], "Do you have any options for 'find'? (e.g. \! -path *.git*)."

  display "Saving configuration data ..."
  write_default_config!(default_config)
  write_config!(config)
  display "All done! The configuration file is stored in '#{config_path}'."
  display "You may now use 'ssync sync' to syncronise your files to the S3 bucket."
end