Class: Questions

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

Instance Method Summary collapse

Instance Method Details

#instance_action(selected_instance) ⇒ Object



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
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
# File 'lib/questions.rb', line 60

def instance_action(selected_instance)
  @selected_instance = selected_instance
  # Choose an Action
  choose do | menu |
  
    menu.prompt = "What would you like to do?"
    unless @selected_instance[:instance_state] == "stopped" || @selected_instance[:instance_state] == "terminated"
      
      #Sets @path_to_keypairs for access
      @path_to_keypairs = StartingConfiguration.path_to_keypair
      menu.choice("SSH"){
          system "ssh -i #{@path_to_keypairs}#{@selected_instance[:keypair]}.pem ec2-user@#{@selected_instance[:ip_address]}"
          BaseMethods.start_over(@@region)
      }
    end
    menu.choice("Refresh"){
      # Actually pull new data
      table = Terminal::Table.new :rows => @selected_instance
      puts table
      # Back to the beginning!
      self.instance_action(selected_instance)
    }
  
    if @selected_instance[:instance_state] == "running"
      menu.choice("Terminate"){
        choose do | menu2 |
          menu2.header = "Are you sure?"
  
          menu2.choice("Yes"){
            BaseMethods.instance_action(@selected_instance[:instance_id], "terminate", @@region)
            say("Instance #{@selected_instance[:instance_id]} has been TERMINATED")
            self.instance_action(selected_instance)
          }
          menu2.choice("No"){
            self.instance_action(selected_instance)
          }
        end
      }
      menu.choice("Stop"){
        choose do | menu2 |
          menu2.header = "Are you sure?"
          menu2.choice("Yes"){
            BaseMethods.instance_action(@selected_instance[:instance_id], "stop", @@region)
            say("Instance #{@selected_instance[:instance_id]} has been Stopped")
            self.instance_action(selected_instance)
          }
          menu2.choice("No"){
            self.instance_action(selected_instance)
          }
        end
      }
    elsif @selected_instance[:instance_state] == "stopped"
      menu.choice("Start"){
        BaseMethods.instance_action(@selected_instance[:instance_id], "start", @@region)
        say("Instance #{@selected_instance[:instance_id]} has been STARTED")
        self.instance_action(selected_instance)
      }
      menu.choice("Terminate"){
        choose do | menu2 |
          menu2.header = "Are you sure?"
          menu2.choice("Yes"){
            BaseMethods.instance_action(@selected_instance[:instance_id], "terminate", @@region)
            say("Instance #{@selected_instance[:instance_id]} has been TERMINATED")
            self.instance_action(selected_instance)
          }
          menu2.choice("No"){
            puts "No action taken"
            self.instance_action(selected_instance)
          }
        end
      }
    end
    
    menu.choice("Back to instances in #{@@region}"){
      BaseMethods.start_over(@@region)
    }
    menu.choice("Change Regions"){
      @@region=nil
      BaseMethods.start_over("none")
    }
  
    menu.choice("Exit!"){
      exit
    }
end
end

#regionObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/questions.rb', line 2

def region
  
  #Select a region
  choose do |menu|
  
    menu.header= "Select a region"
  
    BaseMethods.describe_regions.each do | region |
      menu.choice(region){
        @@region = region
        return region
      }
    end
  
    menu.choice("Exit!"){
      exit
    }
  end
  
end

#select_instance(region) ⇒ Object



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
# File 'lib/questions.rb', line 23

def select_instance(region)
  # Select an instance
  @instances = BaseMethods.describe_instance_by_region(region)
  
  choose do |menu|
    if @instances == []
      menu.prompt = "No instances in this region"
    else
      menu.prompt = "Select an instance"
  
      @instances.each do | instance |
  
        hash = {
          :instance_id => instance[:instance_id],
          :status => instance[:instance_state],
          :name => instance[:name],
          :launch_time => instance[:launch_time]
        }
  
        table = Terminal::Table.new :rows => hash
        menu.choice(table){
          table = Terminal::Table.new :rows => instance
          puts table
          @selected_instance = instance
        }
      end
    end
    menu.choice("Change Regions"){
      BaseMethods.start_over("none")
    }
  
    menu.choice("Exit!"){
      exit
    }
  end
end