use print() - import print_function - should be fine for both py2 and py3
[nepi.git] / examples / planetlab / update_fedora_repo.py
1 #!/usr/bin/env python
2 #
3 #    NEPI, a framework to manage network experiments
4 #    Copyright (C) 2013 INRIA
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU General Public License version 2 as
8 #    published by the Free Software Foundation;
9 #
10 #    This program is distributed in the hope that it will be useful,
11 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
12 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 #    GNU General Public License for more details.
14 #
15 #    You should have received a copy of the GNU General Public License
16 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 #
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
19
20 # THIS IS A MAINTAINANCE SCRIPT!
21 # Use this script only if you are getting the following error when using yum:
22 #
23 #   Error: Cannot retrieve repository metadata (repomd.xml) for repository: fedora. Please verify its path and try again
24
25 # Example of how to run this experiment (replace with your information):
26 #
27 # $ cd <path-to-nepi>
28 # python examples/planetlab/update_fedora_repo.py -H <host1,host2,..> -s <pl-slice> -u <pl-user> -p <pl-password> -k <pl-ssh-key>  
29
30 from __future__ import print_function
31
32 from nepi.execution.ec import ExperimentController 
33
34 from optparse import OptionParser, SUPPRESS_HELP
35 import os
36
37 pl_slice = os.environ.get("PL_SLICE")
38 pl_user = os.environ.get("PL_USER")
39 pl_pass = os.environ.get("PL_PASS")
40 identity = os.environ.get("PL_SSHKEY")
41
42 usage = ("usage: %prog -H <hosts> -s <pl-slice> -u <pl-user> -p <pl-password> "
43         "-k <pl-ssh-key>")
44
45 parser = OptionParser(usage = usage)
46 parser.add_option("-H", "--hosts", dest="hosts",
47         help="Comma separated list of PlanetLab host to update", type="str")
48 parser.add_option("-s", "--pl-slice", dest="pl_slice",
49         help="PlanetLab slicename", type="str", default=pl_slice)
50 parser.add_option("-u", "--pl-user", dest="pl_user",
51         help="PlanetLab web username", type="str", default=pl_user)
52 parser.add_option("-p", "--pl-password", dest="pl_password",
53         help="PlanetLab web password", type="str", default=pl_pass)
54 parser.add_option("-k", "--pl-ssh-key", dest="pl_ssh_key",
55         help="Path to private SSH key associated with the PL account",
56         type="str", default=identity)
57
58 (options, args) = parser.parse_args()
59
60 proceed = raw_input ("Executing this script will modify the fedora yum repositories in the selected PlanetLab hosts. Are you sure to continue? [y/N] ")
61 if proceed.lower() not in ['yes', 'y']:
62     os._exit(1)
63
64 pl_slice = options.pl_slice
65 pl_ssh_key = options.pl_ssh_key
66 pl_user = options.pl_user
67 pl_password = options.pl_password
68 hosts = options.hosts
69
70 hosts = map(str.strip, hosts.split(","))
71 apps = []
72
73 ## Create the experiment controller
74 ec = ExperimentController(exp_id = "pl_update_fedora_repo")
75
76 for hostname in hosts:
77     # Register a Planetlab Node with no restrictions, it can be any node
78     node = ec.register_resource("planetlab::Node")
79     # The username in this case is the slice name, the one to use for login in 
80     # via ssh into PlanetLab nodes. Replace with your own slice name.
81     if hostname != "any":
82         ec.set(node, "hostname", hostname)
83
84     ec.set(node, "username", pl_slice)
85     ec.set(node, "identity", pl_ssh_key)
86     # The pluser and plpassword are the ones used to login in the PlanetLab web 
87     # site. Replace with your own user and password account information.
88     if pl_user:
89         ec.set(node, "pluser", pl_user)
90     if pl_password: 
91         ec.set(node, "plpassword", pl_password)
92
93     ec.set(node, "critical", False)
94     # Remove previous results
95     ec.set(node, "cleanExperiment", True)
96     ec.set(node, "cleanProcesses", True)
97
98     # Application to update fedora.repo
99     path_to_repo = os.path.join(
100             os.path.dirname(os.path.realpath(__file__)),
101             "fedora.repo")
102
103     app = ec.register_resource("linux::Application")
104     ec.set(app, "files", path_to_repo)
105     ec.set(app, "sudo", True) 
106     ec.set(app, "command", 
107         "cp /etc/yum.repos.d/fedora.repo /etc/yum.repos.d/fedora.repo.old; "
108         "cp ${SHARE}/fedora.repo /etc/yum.repos.d/fedora.repo")
109     ec.set(app, "critical", False)
110     ec.register_connection(node, app)
111
112     apps.append(app)
113
114     # Application to update fedora.repo
115     path_to_repo = os.path.join(
116             os.path.dirname(os.path.realpath(__file__)),
117             "fedora-updates.repo")
118
119     app = ec.register_resource("linux::Application")
120     ec.set(app, "files", path_to_repo)
121     ec.set(app, "sudo", True) 
122     ec.set(app, "command", 
123         "cp /etc/yum.repos.d/fedora-updates.repo /etc/yum.repos.d/fedora-updates.repo.old; "
124         "cp ${SHARE}/fedora-updates.repo /etc/yum.repos.d/fedora-updates.repo")
125     ec.set(app, "critical", False)
126     ec.register_connection(node, app)
127     
128     apps.append(app)
129
130
131 ec.deploy()
132
133 ec.wait_finished(apps)
134
135 for app in apps:
136     try:
137         print(ec.trace(app, "stderr"))
138     except:
139         print("NO stderr")
140
141 ec.shutdown()
142