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