Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix executeGHEConsole() / ReportOrgOwners for Hubble service #40

Merged
merged 1 commit into from
Nov 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 25 additions & 18 deletions updater/reports/Report.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,32 @@ def scriptPath(self, script):
script
)

# Executes a script but prints stderr and returns stdout only
# Executes a script but prints stderr and returns stdout only.
# A script is either a path to a file or a list of strings with bash
# commands.
# In case of a remote run (IOW: scripts run via SSH), the script is
# executed by passing its content via stdin to `bash -s --`. This
# method only works if the script has no stdin not set already.
def executeScript(self, script, stdin = None):
if self.configuration["remoteRun"]["enabled"]:
try:
with open(script) as f:
assert(stdin == None)
stdin = f.read()
command = ["bash -s", "--"]
except:
command = script
if not stdin:
try:
# If the script is a file, then read its content as-is into stdin
with open(script) as f:
stdin = f.read()
except:
# If the script is a list of strings, then escape the content and set it to stdin
stdin = " ".join(map(lambda x: '"' + x.replace('\\"', '\\\\"').replace('"', '\\"') + '"', script))
script = ["bash -s", "--"]

# Execute the script via SSH
script = [
"ssh",
"-i", self.configuration["remoteRun"]["sshKey"],
"-p", "122",
"admin@" + self.configuration["remoteRun"]["gheHost"],
] + command
"ssh",
"-i", self.configuration["remoteRun"]["sshKey"],
"-p", "122",
"admin@" + self.configuration["remoteRun"]["gheHost"]
] + script

stdout, stderr = executeCommand(script, stdin)

print(stderr.decode("utf-8"), file = sys.stderr)
Expand All @@ -101,12 +111,9 @@ def executeScript(self, script, stdin = None):
return stdout

def executeGHEConsole(self, rubyCode):
escapedRubyCode = rubyCode.replace('\\t', '\\\\t') \
.replace('\\n', '\\\\n') \
.replace('"', '\\"')
escapedRubyCode = rubyCode.replace('\\t', '\\\\t').replace('\\n', '\\\\n')
return self.executeScript(
["bash -s", "--"],
"github-env bin/runner -e production \"'" + escapedRubyCode + "'\""
["github-env", "bin/runner", "-e", "production", "'" + escapedRubyCode + "'"]
)

# Executes a database query, given as a string
Expand Down
4 changes: 2 additions & 2 deletions updater/reports/ReportOrgOwners.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ def updateData(self):
self.executeGHEConsole('''
puts "organization\towner(s)\n"
User.where(:type => "Organization")
.where("''' + self.andExcludedEntities("login", "").replace('"', '\\\\"') + '''")
.where("''' + self.andExcludedEntities("login", "").replace('"', '\\"') + '''")
.order("login")
.each do |org|
owners = org.admins.where(:disabled => false, :gh_role => nil)
.where("''' + self.andExcludedUsers("login", "").replace('"', '\\\\"') + '''")
.where("''' + self.andExcludedUsers("login", "").replace('"', '\\"') + '''")
.order("login")
.join(",")
puts "#{org.login}\t#{owners}\n"
Expand Down