~cbehrens/agent-smith/static-xenstore

« back to all changes in this revision

Viewing changes to scripts/set_hostname.sh

  • Committer: Chris Behrens
  • Date: 2010-11-12 06:26:29 UTC
  • mfrom: (99.1.6 resolv-fixes)
  • Revision ID: chris.behrens@rackspace.com-20101112062629-j0c2837yh2r3klia
Merged lp:~cbehrens/agent-smith/resolv-fixes

/etc/resolv.conf is created now
hostname setting should work now
Agent upgrade supports /var/run/agent-smith.tar.gz as well as .tar
Bumped agent rev to 0.0.0.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
23
23
hostname="$1"
24
24
firstip="$2"
25
25
 
 
26
if [ -z "$hostname" -o -z "$firstip" ] ; then
 
27
  echo "Usage: set_hostname.sh <hostname> <first_ip>"
 
28
  exit 1
 
29
fi
 
30
 
26
31
update_etc_hostname() {
27
 
        echo ${hostname} > "${ROOT}/etc/hostname"
 
32
  echo ${hostname} > "${ROOT}/etc/hostname"
28
33
}
29
34
 
30
35
update_etc_hosts() {
31
 
        # This is tricky
32
 
    # We need to be careful not to scribble over the existing entries
33
 
    # in /etc/hosts, but also be thorough enough to actually make it work
34
 
    backupfile="$(make_backup "${ROOT}/etc/hosts")"
35
 
    if ! [ -f "${backupfile}" ]
 
36
  # This is tricky
 
37
  # We need to be careful not to scribble over the existing entries
 
38
  # in /etc/hosts, but also be thorough enough to actually make it work
 
39
  backupfile="$(make_backup "${ROOT}/etc/hosts")"
 
40
  if ! [ -f "${backupfile}" ]
 
41
  then
 
42
    exit 1
 
43
  fi
 
44
 
 
45
  found=`awk -v ip=$firstip '$1 == ip { print "found"; exit 0 }' < ${ROOT}/etc/hosts`
 
46
  if [ "x$found" != "xfound" ] ; then
 
47
    echo "$firstip $hostname" >> ${ROOT}/etc/hosts
 
48
    return 0
 
49
  fi
 
50
 
 
51
  cat "${backupfile}" | while read ip tail
 
52
  do
 
53
    if [ "x$ip" = "x${firstip}" ]
36
54
    then
37
 
        exit 1
38
 
    fi
39
 
        cat "${backupfile}" | while read ip tail
40
 
        do
41
 
                if [ "x$ip" = "x$firstip" ]
42
 
                then
43
 
            # If there's just a single hostname, assume it's the one we
44
 
            # need to replace.
45
 
                        if echo "$tail" | grep -iq '^[a-z][a-z0-9]*$'
46
 
                        then
47
 
                echo "#$ip $tail # Removed by Agent Smith"
48
 
                echo "$ip $hostname"
49
 
            elif echo "$tail" | grep -q '[      ]'
50
 
                        then
51
 
                                # Ok, more than one word in the line..
52
 
                # Without the while loop, a, b, and c are not available
53
 
                # afterwards
54
 
                                echo "$tail" | while read a b c
55
 
                                do
56
 
                    if [ -n "$c" ] && ! echo "$c" | grep -q "^#"
57
 
                    then
58
 
                        # Three words.. Don't know how to handle that.
59
 
                        echo "$ip $tail"
60
 
                    elif echo "$a" | grep -q '\.' && echo echo "$b" | grep -q '\.'
61
 
                    then
62
 
                        # Two words, both with a dot in them.. Don't know how to handle that.
63
 
                        echo "$ip $tail"
64
 
                    elif ! echo "$a" | grep -q '\.' && ! echo echo "$b" | grep -q '\.'
65
 
                    then
66
 
                        # Two words, neither with a dot in them.. Don't know how to handle that.
67
 
                        echo "$ip $tail"
68
 
                    else
69
 
                        # If there's not a dot in both, nor are both missing a dot,
70
 
                        # exactly one of them must have a dot. Yay.
71
 
                        withdot="$(echo $a | grep '\.' ; echo $b | grep '\.')"
72
 
                        withoutdot="$(echo $a | grep -v '\.' ; echo $b | grep -v '\.')"
73
 
                        if [ "${withdot%%.*}" != "${withoutdot}" ]
74
 
                        then
75
 
                            # If the host part doesn't match, I don't know what to do.
76
 
                            echo "$ip $tail"
77
 
                        else
78
 
                            domain="${withdot#*.}"
79
 
                            echo "#$ip $tail # Removed by Agent Smith"
80
 
                            echo "$ip $hostname.$domain $hostname"
81
 
                        fi
82
 
                    fi
83
 
                done
 
55
      # If there's just a single hostname, assume it's the one we
 
56
      # need to replace.
 
57
      if echo "$tail" | egrep -iq '^[a-z](\-|[a-z0-9])*$'
 
58
      then
 
59
        echo "#$ip $tail # Removed by Agent Smith"
 
60
        echo "$ip $hostname"
 
61
      elif echo "$tail" | grep -q '[    ]'
 
62
      then
 
63
        # Ok, more than one word in the line..
 
64
        # Without the while loop, a, b, and c are not available
 
65
        # afterwards
 
66
        echo "$tail" | while read a b c
 
67
        do
 
68
          if [ -n "$c" ] && ! echo "$c" | grep -q "^#"
 
69
          then
 
70
            # Three words.. Don't know how to handle that.
 
71
            echo "$ip $tail"
 
72
          elif echo "$a" | grep -q '\.' && echo echo "$b" | grep -q '\.'
 
73
          then
 
74
            # Two words, both with a dot in them.. Don't know how to handle that.
 
75
            echo "$ip $tail"
 
76
          elif ! echo "$a" | grep -q '\.' && ! echo echo "$b" | grep -q '\.'
 
77
          then
 
78
            # Two words, neither with a dot in them.. Don't know how to handle that.
 
79
            echo "$ip $tail"
 
80
          else
 
81
            # If there's not a dot in both, nor are both missing a dot,
 
82
            # exactly one of them must have a dot. Yay.
 
83
            withdot="$(echo $a | grep '\.' ; echo $b | grep '\.')"
 
84
            withoutdot="$(echo $a | grep -v '\.' ; echo $b | grep -v '\.')"
 
85
            if [ "${withdot%%.*}" != "${withoutdot}" ]
 
86
            then
 
87
              # If the host part doesn't match, I don't know what to do.
 
88
              echo "$ip $tail"
84
89
            else
85
 
                echo "$ip $tail"
 
90
              domain="${withdot#*.}"
 
91
              echo "#$ip $tail # Removed by Agent Smith"
 
92
              echo "$ip $hostname.$domain $hostname"
86
93
            fi
87
 
        else
88
 
            echo "$ip $tail"
89
 
        fi
90
 
    done > "${ROOT}/etc/hosts"
 
94
          fi
 
95
        done
 
96
      else
 
97
        echo "$ip $tail"
 
98
      fi
 
99
    else
 
100
      echo "$ip $tail"
 
101
    fi
 
102
  done > "${ROOT}/etc/hosts"
91
103
}
92
104
 
93
105
update_etc_hostname
94
106
update_etc_hosts
95
107
 
 
108
if [ "$ROOT" = "/" -o "$ROOT" = "" ] ; then
 
109
  # only do this if we're not running tests
 
110
  hostname $hostname
 
111
fi
 
112
 
96
113
exit 0