4 # Licensed under the Apache License, Version 2.0 (the "License");
5 # you may not use this file except in compliance with the License.
6 # You may obtain a copy of the License at
8 # http://www.apache.org/licenses/LICENSE-2.0
10 # Unless required by applicable law or agreed to in writing, software
11 # distributed under the License is distributed on an "AS IS" BASIS,
12 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 # See the License for the specific language governing permissions and
14 # limitations under the License.
20 class Stasher(object):
21 def __init__(self, args=None):
23 self.backupfilename = "configuration.tar.gz"
26 self.push_workspace_to_remote(toserver=self.args.remotehost,
27 todirectory=self.args.remotedir,
28 workspace=self.args.workspace)
30 def push_workspace_to_remote(self, toserver, todirectory, workspace):
31 """ Move workspace backup to remote host """
32 destination = toserver + ":" + todirectory
33 sshoptions = 'ssh -o stricthostkeychecking=no -o userknownhostsfile=/dev/null -o batchmode=yes -o passwordauthentication=no'
34 sourceconfiguration = os.path.join(workspace, self.backupfilename)
35 sourcerpm = os.path.join(workspace, "buildrepository")
36 rsyncpathval = "mkdir -p " + todirectory + " && rsync"
37 cmd = ["/usr/bin/rsync",
40 "--rsync-path", rsyncpathval,
42 sourceconfiguration, sourcerpm,
45 print subprocess.check_output(cmd, shell=False, stderr=subprocess.STDOUT)
46 except subprocess.CalledProcessError as err:
47 raise StasherError("Following command retured code %d: %s" % (err.returncode,
50 class StasherError(Exception):
51 """ Exceptions originating from builder """
56 class ArgumentRemote(object):
57 """ Default arguments which are always needed """
59 """ Create parser for arguments """
60 self.parser = argparse.ArgumentParser(description='Workspace stasher copies workspace to remote host.')
61 self.set_arguments(self.parser)
62 self.parser.add_argument("--workspace",
63 help="Local (source) directory",
66 def set_arguments(self, parser):
67 """ Add extra arguments to parser """
68 parser.add_argument("--remotehost",
69 help="Remote host where script will ssh/rsync to store build",
71 parser.add_argument("--remotedir",
72 help="Remote directory to use for storing build",
77 """ Get arguments required for stashing local workspace """
78 args = ArgumentRemote().parser.parse_args()
80 stasher = Stasher(args)
83 if __name__ == "__main__":