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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 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']
  cmd = "ssh -p #{port} #{user}@#{ip}"
  exp = RubyExpect::Expect.spawn("#{cmd}")
  exp.procedure do
    any do
      expect /Permission denied/ do
        puts "this user is not allowed"
      end

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

      expect /continue connecting/ do
        send "yes"
      end

      expect /\$\s+$/ do
        puts "login successfully and press return to continue"
        interact
      end

      expect /\#\s+$/ do
        puts "login successfully and press return to continue"
        interact
      end

      expect /assword/ do
        cmd = "ssh-copy-id -i #{user}@#{ip}"
        exp2 = RubyExpect::Expect.spawn("#{cmd}")
        exp2.procedure do
          any do
            expect /Permission denied/ do
              puts "this user is not allowed"
            end

            expect /continue connecting/ do
              send "yes"
            end

            expect /assword/ do
              send "#{password}"
              sleep 3
              puts "now you can try again, or use |ssh #{user}@#{ip}| if failed please check your password"
              exit 0
            end

            run
          end
        end
      end

      run
    end
  end
  else
    puts "project does not exist"
    exit 1
  end
end