67c82a3b9297a9cd53a8abc2fe159e6b5db61b9f
[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 nepi.execution.ec import ExperimentController 
31
32 from optparse import OptionParser, SUPPRESS_HELP
33 import os
34
35 pl_slice = os.environ.get("PL_SLICE")
36 pl_user = os.environ.get("PL_USER")
37 pl_pass = os.environ.get("PL_PASS")
38 identity = os.environ.get("PL_SSHKEY")
39
40 usage = ("usage: %prog -H <hosts> -s <pl-slice> -u <pl-user> -p <pl-password> "
41         "-k <pl-ssh-key>")
42
43 parser = OptionParser(usage = usage)
44 parser.add_option("-H", "--hosts", dest="hosts",
45         help="Comma separated list of PlanetLab host to update", type="str")
46 parser.add_option("-s", "--pl-slice", dest="pl_slice",
47         help="PlanetLab slicename", type="str", default=pl_slice)
48 parser.add_option("-u", "--pl-user", dest="pl_user",
49         help="PlanetLab web username", type="str", default=pl_user)
50 parser.add_option("-p", "--pl-password", dest="pl_password",
51         help="PlanetLab web password", type="str", default=pl_pass)
52 parser.add_option("-k", "--pl-ssh-key", dest="pl_ssh_key",
53         help="Path to private SSH key associated with the PL account",
54         type="str", default=identity)
55
56 (options, args) = parser.parse_args()
57
58 proceed = raw_input ("Executing this script will modify the fedora yum repositories in the selected PlanetLab hosts. Are you sure to continue? [y/N] ")
59 if proceed.lower() not in ['yes', 'y']:
60     os._exit(1)
61
62 pl_slice = options.pl_slice
63 pl_ssh_key = options.pl_ssh_key
64 pl_user = options.pl_user
65 pl_password = options.pl_password
66 hosts = options.hosts
67
68 hosts = map(str.strip, hosts.split(","))
69 apps = []
70
71 ## Create the experiment controller
72 ec = ExperimentController(exp_id = "pl_update_fedora_repo")
73
74 for hostname in hosts:
75     # Register a Planetlab Node with no restrictions, it can be any node
76     node = ec.register_resource("planetlab::Node")
77     # The username in this case is the slice name, the one to use for login in 
78     # via ssh into PlanetLab nodes. Replace with your own slice name.
79     if hostname != "any":
80         ec.set(node, "hostname", hostname)
81
82     ec.set(node, "username", pl_slice)
83     ec.set(node, "identity", pl_ssh_key)
84     # The pluser and plpassword are the ones used to login in the PlanetLab web 
85     # site. Replace with your own user and password account information.
86     if pl_user:
87         ec.set(node, "pluser", pl_user)
88     if pl_password: 
89         ec.set(node, "plpassword", pl_password)
90
91     ec.set(node, "critical", False)
92     # Remove previous results
93     ec.set(node, "cleanExperiment", True)
94     ec.set(node, "cleanProcesses", True)
95
96     # Application to update fedora.repo
97     path_to_repo = os.path.join(
98             os.path.dirname(os.path.realpath(__file__)),
99             "fedora.repo")
100
101     app = ec.register_resource("linux::Application")
102     ec.set(app, "files", path_to_repo)
103     ec.set(app, "sudo", True) 
104     ec.set(app, "command", 
105         "cp /etc/yum.repos.d/fedora.repo /etc/yum.repos.d/fedora.repo.old; "
106         "cp ${SHARE}/fedora.repo /etc/yum.repos.d/fedora.repo")
107     ec.set(app, "critical", False)
108     ec.register_connection(node, app)
109
110     apps.append(app)
111
112     # Application to update fedora.repo
113     path_to_repo = os.path.join(
114             os.path.dirname(os.path.realpath(__file__)),
115             "fedora-updates.repo")
116
117     app = ec.register_resource("linux::Application")
118     ec.set(app, "files", path_to_repo)
119     ec.set(app, "sudo", True) 
120     ec.set(app, "command", 
121         "cp /etc/yum.repos.d/fedora-updates.repo /etc/yum.repos.d/fedora-updates.repo.old; "
122         "cp ${SHARE}/fedora-updates.repo /etc/yum.repos.d/fedora-updates.repo")
123     ec.set(app, "critical", False)
124     ec.register_connection(node, app)
125     
126     apps.append(app)
127
128
129 ec.deploy()
130
131 ec.wait_finished(apps)
132
133 for app in apps:
134     try:
135         print ec.trace(app, "stderr")
136     except:
137         print "NO stderr"
138
139 ec.shutdown()
140