Class: JenkinsShell

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

Defined Under Namespace

Classes: Error

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port = 8080, ssl = false, username = nil, password = nil) ⇒ JenkinsShell

Returns a new instance of JenkinsShell.



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/jenkins_shell.rb', line 82

def initialize(
    host,
    port = 8080,
    ssl = false,
    username = nil,
    password = nil
)
    @cookie = nil
    @crumb = nil
    @int_cwd = "."
    @host = host.gsub(/^https?:\/\/|(:[0-9]+)?\/.+/, "")
    @password = password
    @port = port
    @ssl = ssl
    @username = username

    # Initialize @cwd
    @cwd = pwd
end

Instance Attribute Details

Returns the value of attribute cookie.



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

def cookie
  @cookie
end

#crumbObject (readonly)

Returns the value of attribute crumb.



7
8
9
# File 'lib/jenkins_shell.rb', line 7

def crumb
  @crumb
end

#cwdObject (readonly)

Returns the value of attribute cwd.



8
9
10
# File 'lib/jenkins_shell.rb', line 8

def cwd
  @cwd
end

#hostObject (readonly)

Returns the value of attribute host.



9
10
11
# File 'lib/jenkins_shell.rb', line 9

def host
  @host
end

#passwordObject (readonly)

Returns the value of attribute password.



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

def password
  @password
end

#portObject (readonly)

Returns the value of attribute port.



11
12
13
# File 'lib/jenkins_shell.rb', line 11

def port
  @port
end

#usernameObject (readonly)

Returns the value of attribute username.



12
13
14
# File 'lib/jenkins_shell.rb', line 12

def username
  @username
end

Instance Method Details

#cd(dir = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/jenkins_shell.rb', line 14

def cd(dir = nil)
    return true if (dir.nil? || dir.empty?)

    new_cwd = pwd("#{@int_cwd}/#{dir}")

    return false if (new_cwd.empty? || (new_cwd == @cwd))

    @cwd = new_cwd
    @int_cwd = "#{@int_cwd}/#{dir}".gsub(%r{[^/]+/\.\.}, "")

    return true
end

#command(cmd, dir = @int_cwd) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/jenkins_shell.rb', line 27

def command(cmd, dir = @int_cwd)
     if (@username && (@cookie.nil? || @crumb.nil?))
    if (@username && (@cookie.nil? || @crumb.nil?))
        raise JenkinsShell::Error::LoginFailure.new
    end

    # Create Groovy script
    fix = dir.gsub(/\\/, "\\\\\\")
    gs = "println(\"cmd /c cd #{fix} && #{cmd}\".execute().text)"

    # Make POST request and return command output
    xml = post("/script", "script=#{encode(gs)}")[1]
    output = xml.get_elements("html/body/div/div/pre")[1]
    return "" if (output.nil?)
    return output.text.strip
end

#loginObject



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/jenkins_shell.rb', line 102

def 
    get("/login")
    post(
        "/j_acegi_security_check",
        [
            "j_username=#{encode(@username)}",
            "j_password=#{encode(@password)}",
            "Submit=log+in"
        ].join("&")
    )
    get("/")
end

#pwd(dir = @int_cwd) ⇒ Object



152
153
154
155
156
157
# File 'lib/jenkins_shell.rb', line 152

def pwd(dir = @int_cwd)
    command("dir", dir).match(/^\s+Directory of (.+)/) do |m|
        return m[1]
    end
    return ""
end