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

Add get_graph service to fetch minigraph automatically #288

Merged
merged 12 commits into from
Feb 17, 2017
6 changes: 5 additions & 1 deletion build_debian.sh
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ sudo LANG=C DEBIAN_FRONTEND=noninteractive chroot $FILESYSTEM_ROOT apt-get -y in
usbutils \
pciutils \
iptables-persistent \
logrotate
logrotate \
wget

## Remove sshd host keys, and will regenerate on first sshd start
sudo rm -f $FILESYSTEM_ROOT/etc/ssh/ssh_host_*_key*
Expand Down Expand Up @@ -241,6 +242,9 @@ EOF

sudo cp files/dhcp/rfc3442-classless-routes $FILESYSTEM_ROOT/etc/dhcp/dhclient-exit-hooks.d
sudo cp files/dhcp/sethostname $FILESYSTEM_ROOT/etc/dhcp/dhclient-exit-hooks.d/
sudo cp files/dhcp/graphserviceurl $FILESYSTEM_ROOT/etc/dhcp/dhclient-exit-hooks.d/
sudo cp files/dhcp/snmpcommunity $FILESYSTEM_ROOT/etc/dhcp/dhclient-exit-hooks.d/
sudo cp files/dhcp/dhclient.conf $FILESYSTEM_ROOT/etc/dhcp/

if [ -f sonic_debian_extension.sh ]; then
./sonic_debian_extension.sh $FILESYSTEM_ROOT
Expand Down
8 changes: 8 additions & 0 deletions files/build_templates/sonic_debian_extension.j2
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,14 @@ sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable interfaces-config.service
sudo cp $IMAGE_CONFIGS/interfaces/interfaces-config.sh $FILESYSTEM_ROOT/usr/bin/
sudo cp $IMAGE_CONFIGS/interfaces/*.j2 $FILESYSTEM_ROOT/etc/sonic/templates/

# Copy initial interfaces configuration file, will be overwritten on first boot
sudo cp $IMAGE_CONFIGS/interfaces/init_interfaces $FILESYSTEM_ROOT/etc/network

# Copy get graph script and service file
sudo cp $IMAGE_CONFIGS/get_graph/get_graph.service $FILESYSTEM_ROOT/etc/systemd/system/
sudo LANG=C chroot $FILESYSTEM_ROOT systemctl enable get_graph.service
sudo cp $IMAGE_CONFIGS/get_graph/get_graph $FILESYSTEM_ROOT/usr/bin/

# Copy SNMP configuration files
sudo cp $IMAGE_CONFIGS/snmp/snmp.yml $FILESYSTEM_ROOT/etc/sonic/

Expand Down
24 changes: 24 additions & 0 deletions files/dhcp/dhclient.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Configuration file for /sbin/dhclient, which is included in Debian's
# dhcp3-client package.
#
# This is a sample configuration file for dhclient. See dhclient.conf's
# man page for more information about the syntax of this file
# and a more comprehensive list of the parameters understood by
# dhclient.
#
# Normally, if the DHCP server provides reasonable information and does
# not leave anything out (like the domain name, for example), then
# few changes must be made to this file, if any.
#

option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;
option snmp-community code 224 = text;
option minigraph-service-url code 225 = text;
Copy link
Collaborator

@lguohan lguohan Feb 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minigraph-service-url [](start = 7, length = 21)

to simplify the name, we can use minigraph-url #Resolved


send host-name = gethostname();
request subnet-mask, broadcast-address, time-offset, routers,
domain-name, domain-name-servers, domain-search, host-name,
dhcp6.name-servers, dhcp6.domain-search,
netbios-name-servers, netbios-scope, interface-mtu,
rfc3442-classless-static-routes, ntp-servers, snmp-community, minigraph-service-url;

11 changes: 11 additions & 0 deletions files/dhcp/graphserviceurl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
case $reason in
BOUND|RENEW|REBIND|REBOOT)
if [ -n "${new_minigraph_service_url}" ]; then
echo $new_minigraph_service_url > /etc/sonic/graph_service_url
else
if [ !-f "/etc/sonic/graph_service_url"]; then
echo "N/A" > /etc/sonic/graph_service_url
fi
fi
;;
esac
15 changes: 7 additions & 8 deletions files/dhcp/sethostname
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
#!/bin/bash
# This script is to update hostname of the system.

if [ "$reason" != BOUND ] && [ "$reason" != RENEW ] \
&& [ "$reason" != REBIND ] && [ "$reason" != REBOOT ]
if [ "$reason" = BOUND ] && ["$reason" != RENEW ] \
&& ["$reason" != REBIND ] && ["$reason" != REBOOT ]
then
exit 0
fi

current_host_name=`hostname -s`

if [ "$current_host_name" != "$new_host_name" ]
if [ "$current_host_name" != "$new_host_name" ] && [ ! -z "$new_host_name" ]
then
echo $new_host_name > /etc/hostname
line_to_replace=`grep 127.0.0.1.*$current_host_name /etc/hosts`
new_line=`echo $line_to_replace | sed "s/$current_host_name/$new_host_name/"`
sed -i "s/$line_to_replace/$new_line/" /etc/hosts

hostname -F /etc/hostname
sed -i "/\s$current_host_name$/d" /etc/hosts
echo "127.0.0.1 $new_host_name" >> /etc/hosts
fi
Copy link
Collaborator

@lguohan lguohan Feb 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are we changing this file at all? #ByDesign

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From observation this bash will be called twice in a row sometimes, "oldhostname" -> "" and "" -> "newhostname". Old implementation is not working correctly under these cases.


In reply to: 100569696 [](ancestors = 100569696)



11 changes: 11 additions & 0 deletions files/dhcp/snmpcommunity
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
case $reason in
BOUND|RENEW|REBIND|REBOOT)
if [ -n "${new_snmp_community}" ]; then
if [ -f /etc/sonic/snmp.yml ]; then
sed -i "s/^snmp_rocommunity:.*/snmp_rocommunity: $new_snmp_community/g" /etc/sonic/snmp.yml
else
echo "snmp_rocommunity: "$new_snmp_community > /etc/sonic/snmp.yml
fi
fi
;;
esac
26 changes: 26 additions & 0 deletions files/image_config/get_graph/get_graph
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#!/bin/bash

while [ ! -f /etc/sonic/graph_service_url ]; do
echo "No graph service url speficied, waiting for DHCP response..."
Copy link
Collaborator

@lguohan lguohan Feb 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

echo "No graph service url speficied, waiting for DHCP response..." [](start = 4, length = 67)

I am not sure where I am able to see the message? is it in syslog? #ByDesign

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, syslog.


In reply to: 100567923 [](ancestors = 100567923)

sleep 1
done
Copy link
Collaborator

@lguohan lguohan Feb 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where is the graph service configuration file to enable/disable graph downloading? #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/etc/sonic/graph_service_url. A non-existing file means dhcp, and an exisiting file writing "disabled" means disabled.


In reply to: 100565302 [](ancestors = 100565302)


if [ `cat /etc/sonic/graph_service_url` = "N/A" ]; then
echo "Graph service url not included in DHCP response, skip minigraph downloading"
exit 0
fi

if [ -f /etc/sonic/minigraph.xml ]; then
echo "Renaming minigraph.xml to minigraph.old"
rm -rf /etc/sonic/minigraph.old
mv /etc/sonic/minigraph.xml /etc/sonic/minigraph.old
Copy link
Collaborator

@lguohan lguohan Feb 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mv /etc/sonic/minigraph.xml /etc/sonic/minigraph.old [](start = 4, length = 52)

this will replace the old, no need to rm -rf #Resolved

fi

HOSTNAME=`hostname -s`
GRAPH_URL=`sed "s/{{\s*hostname\s*}}/$HOSTNAME/g" /etc/sonic/graph_service_url`
Copy link
Collaborator

@lguohan lguohan Feb 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you use jinji2 template to instead of sed? #Resolved

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't have j2 installed in base image now. Do you want to install it?


In reply to: 100561378 [](ancestors = 100561378)

echo "Getting minigraph from $GRAPH_URL"

while true; do
wget -T 15 -c $GRAPH_URL -O /etc/sonic/minigraph.xml && break
Copy link
Collaborator

@lguohan lguohan Feb 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wget [](start = 4, length = 4)

I think we should use curl here, the package is much smaller. #Resolved

Copy link
Collaborator

@lguohan lguohan Feb 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wget -T 15 -c $GRAPH_URL -O /etc/sonic/minigraph.xml && break [](start = 4, length = 61)

print message for each try. #WontFix

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wget itself will print messages.


In reply to: 100567987 [](ancestors = 100567987)

sleep 5
done
12 changes: 12 additions & 0 deletions files/image_config/get_graph/get_graph.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[Unit]
Description=get minigraph
Copy link
Collaborator

@lguohan lguohan Feb 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get minigraph [](start = 12, length = 13)

Download minigraph from graph service #Resolved

Before=ntp-config.service
Before=rsyslog-config.service
Before=interfaces-config.service

[Service]
Type=oneshot
ExecStart=/usr/bin/get_graph
Copy link
Collaborator

@lguohan lguohan Feb 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_graph [](start = 19, length = 9)

updategraph #Resolved


[Install]
WantedBy=multi-user.target
Copy link
Collaborator

@lguohan lguohan Feb 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you also change the get_graph.service name to updategraph.service? #Resolved

11 changes: 11 additions & 0 deletions files/image_config/interfaces/init_interfaces
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Initial /etc/network/interface file for first boot
# Will be overwritten based on minigraph information by interfaces-config service

# The loopback network interface
auto lo
iface lo inet loopback
#
# The management network interface
auto eth0
iface eth0 inet dhcp
Copy link
Collaborator

@lguohan lguohan Feb 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't this the default debian behavior? do we need to override? #WontFix

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not quite sure whether this is the default behavior. Why not override it explicitly to make things clearer though?


In reply to: 100564200 [](ancestors = 100564200)


2 changes: 1 addition & 1 deletion files/image_config/interfaces/interfaces-config.service
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[Unit]
Description=Update interfaces configuration
Before=network.target
Before=database.service

[Service]
Type=oneshot
Expand Down
3 changes: 3 additions & 0 deletions files/image_config/interfaces/interfaces-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
sonic-cfggen -m /etc/sonic/minigraph.xml -t /etc/sonic/templates/interfaces.j2 >/etc/network/interfaces
sonic-cfggen -m /etc/sonic/minigraph.xml -t /etc/sonic/templates/vlan_interfaces.j2 >/etc/network/interfaces.d/vlan_interfaces
sonic-cfggen -m /etc/sonic/minigraph.xml -t /etc/sonic/templates/lag_interfaces.j2 >/etc/network/interfaces.d/lag_interfaces
ifdown eth0 && ifup eth0
ifdown lo && ifup lo