Class: Core_p

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project, server_alias) ⇒ Core_p

Returns a new instance of Core_p.



8
9
10
11
12
13
14
15
# File 'lib/core.rb', line 8

def initialize(project, server_alias)
  self.project = project
  self.server_alias = server_alias
  if !File.exist?("#{$public_key_path}")
    puts "you dont have a pair of ssh key,we will generate it"
    self.generate_key
  end
end

Instance Attribute Details

#projectObject

Returns the value of attribute project.



3
4
5
# File 'lib/core.rb', line 3

def project
  @project
end

#server_aliasObject

Returns the value of attribute server_alias.



3
4
5
# File 'lib/core.rb', line 3

def server_alias
  @server_alias
end

Instance Method Details

#generate_keyObject

when trying to send public key



18
19
20
21
22
# File 'lib/core.rb', line 18

def generate_key
  puts "please enter return 3 times"
  cmd = "ssh-keygen -t rsa"
  `#{cmd}`
end

#loginObject



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
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/core.rb', line 24

def 
  if Loginx::Exist.project_exist?(self.project)
    config = YAML::load (File.open("#{$config_path}"))
    info = YAML::load(File.open("#{$project_path}/#{self.project}.yml"))
    if !info.has_key?(self.server_alias)
      puts "sorry the server alias does not exist"
      exit 1
    end
    user = config['config']['user']
    port = config['config']['port']
    ip = info[self.server_alias]['ip']
    password = info[self.server_alias]['password']

    puts "do you want to send your ssh key"
    puts "enter return to skip or 'y' to continue to send your key"
    flag = STDIN.gets.chomp
    if flag == 'y'
      cmd2 = "ssh-copy-id -i #{user}@#{ip}"
      #exec "#{cmd2}"
      exp = RubyExpect::Expect.spawn("#{cmd2}")
      exp.procedure do
        any do
          expect /Permission denied/ do
            puts "this user is not allowed"
            exit 0
          end

          expect /continue connecting/ do
            send "yes"
          end

          expect /Connection refused/ do
            puts "please check your port setting or password"
            exit 0
          end

          expect /already exist/ do
            puts "you already added keys before"
            exit 0
          end

          expect /assword/ do
            send "#{password}"
          end
          run
        end
      end
    end

    cmd = "ssh -p #{port} #{user}@#{ip}"
    exec "#{cmd}"
  else
    puts "project does not exist"
    exit 1
  end
end