Class: Strongspace::Command::Spaces

Inherits:
Base show all
Defined in:
lib/strongspace/commands/spaces.rb

Instance Attribute Summary

Attributes inherited from Base

#args

Instance Method Summary collapse

Methods inherited from Base

#initialize, #strongspace

Methods included from PluginInterface

#base_command, #command, included

Methods included from Helpers

#ask, #backup_space?, #bin_folder, #command_name, #computername, #confirm, #confirm_command, #create_pid_file, #credentials_file, #credentials_folder, #delete_pid_file, #display, #error, #format_date, #gui_ssh_key, home_directory, #home_directory, #kill_via_pidfile, #launchd_agents_folder, #logs_folder, #pid_file_path, #pid_from_pid_file, #pids_folder, #plugins_folder, #process_running?, #redisplay, running_on_a_mac?, #running_on_a_mac?, #running_on_windows?, running_on_windows?, #shell, #space_exist?, support_directory, #support_directory

Constructor Details

This class inherits a constructor from Strongspace::Command::Base

Instance Method Details

#createObject



18
19
20
21
22
23
24
# File 'lib/strongspace/commands/spaces.rb', line 18

def create
  name = args[0]
  type = args[1]

  strongspace.create_space(name, type)
  display "Create space #{name}"
end

#create_snapshotObject



48
49
50
51
52
53
54
55
56
57
# File 'lib/strongspace/commands/spaces.rb', line 48

def create_snapshot
  space_name, snapshot_name = args[0..1]

  if snapshot_name.blank?
    snapshot_name = Time.now.strftime("%Y-%m-%d-%H%M%S")
  end

  strongspace.create_snapshot(space_name, snapshot_name)
  display "Created snapshot '#{space_name}@#{snapshot_name}'"
end

#create_snapshot_and_thinObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/strongspace/commands/spaces.rb', line 59

def create_snapshot_and_thin

  retries = 0
  success = false
  while (!success and (retries < 5)) do
    begin
      create_snapshot
      #thin_snapshots
    rescue SocketError => e
      sleep(10)
      retries = retries + 1
      next
    end
    success = true
  end

end

#deleteObject



26
27
28
29
# File 'lib/strongspace/commands/spaces.rb', line 26

def delete
  strongspace.delete_space(args.first)
  display "Space #{args.first} removed."
end

#delete_snapshotObject



77
78
79
80
81
82
# File 'lib/strongspace/commands/spaces.rb', line 77

def delete_snapshot
  space_name, snapshot_name = args[0..1]

  strongspace.delete_snapshot(space_name, snapshot_name)
  display "Destroyed snapshot '#{space_name}@#{snapshot_name}'"
end

#listObject Also known as: index



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/strongspace/commands/spaces.rb', line 3

def list
  long = args.any? { |a| a == '--long' }
  spaces = strongspace.spaces

  if spaces.empty?
    display "#{strongspace.username} has no spaces"
  else
    display "=== #{strongspace.username} has #{spaces.size} space#{'s' if spaces.size > 1}"
    spaces.each do |space|
      display "#{space['name']} [type: #{space['type']}, snapshots: #{space['snapshots']}]"
    end
  end
end

#schedule_snapshotsObject



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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'lib/strongspace/commands/spaces.rb', line 104

def schedule_snapshots
  space_name = args[0]

  if running_on_a_mac?
    plist = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>
    <!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN
    http://www.apple.com/DTDs/PropertyList-1.0.dtd >
    <plist version=\"1.0\">
    <dict>
    <key>Label</key>
    <string>com.strongspace.Snapshots.#{space_name}</string>
    <key>Program</key>
    <string>#{support_directory}/gems/bin/strongspace</string>
    <key>ProgramArguments</key>
    <array>
    <string>strongspace</string>
    <string>spaces:create_snapshot_and_thin</string>
    <string>#{space_name}</string>
    </array>
    <key>KeepAlive</key>
    <false/>
    <key>StartCalendarInterval</key>
    <dict>
      <key>Minute</key>
      <integer>0</integer>
    </dict>
    <key>RunAtLoad</key>
    <true/>
    <key>StandardOutPath</key>
    <string>#{log_file}</string>
    <key>StandardErrorPath</key>
    <string>#{log_file}</string>
    <key>EnvironmentVariables</key>
    <dict>
    <key>STRONGSPACE_DISPLAY</key>
    <string>logging</string>
    <key>GEM_PATH</key>
    <string>#{support_directory}/gems</string>
    <key>GEM_HOME</key>
    <string>#{support_directory}/gems</string>
    <key>RACK_ENV</key>
    <string>production</string>
    </dict>

    </dict>
    </plist>"

    file = File.new(launchd_plist_file(space_name), "w+")
    file.puts plist
    file.close

    r = `launchctl load -S aqua '#{launchd_plist_file(space_name)}'`
    if r.strip.ends_with?("Already loaded")
      error "This task is aready scheduled, unload before scheduling again"
      return
    end
    display "Scheduled Snapshots of #{space_name}"
  end
end

#snapshotsObject



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/strongspace/commands/spaces.rb', line 31

def snapshots
  if args.length == 0
    display "No space specified."
    return
  end
  snapshots = strongspace.snapshots(args.first)

  if snapshots.empty?
    display "Space #{args.first} has no snapshots"
  else
    display "=== Space #{args.first} has #{snapshots.size} snapshot#{'s' if snapshots.size > 1}"
    snapshots.each do |snapshot|
      display "#{args.first}@#{snapshot['name']} [created: #{snapshot['created_at']}]"
    end
  end
end

#thin_snapshotsObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/strongspace/commands/spaces.rb', line 84

def thin_snapshots
  snapshots = strongspace.snapshots(args.first)

  keeplist = []

  snapshots.each do |s|
    if DateTime.parse(s['created_at']).to_local_time > (Time.now - 3600*24)
      keeplist << s
      next
    end
  end

  (snapshots - keeplist).each do |k|
    puts "Drop: " + k['name']
    strongspace.delete_snapshot(args.first, k['name'])
  end

end

#unschedule_snapshotsObject



164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# File 'lib/strongspace/commands/spaces.rb', line 164

def unschedule_snapshots
  space_name = args[0]

  if space_name.blank?
    display "Please supply the name of a space"
    return false
  end

  if running_on_windows?
    error "Scheduling currently isn't supported on Windows"
    return
  end

  if running_on_a_mac?
    if File.exist? launchd_plist_file(space_name)
      `launchctl unload '#{launchd_plist_file(space_name)}'`
      FileUtils.rm(launchd_plist_file(space_name))
    end
  else  # Assume we're running on linux/unix
    CronEdit::Crontab.Remove "strongspace-snapshots-#{space_name}"
  end

end