summaryrefslogtreecommitdiff
path: root/cxmanage_api/cli/commands/eeprom.py
blob: 86ca1cd91ad72f3c1d94ff6dfa9847b27cb877ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
"""Calxeda: eeprom.py """

# Copyright (c) 2012-2013, Calxeda Inc.
#
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are
# met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above copyright
# notice, this list of conditions and the following disclaimer in the
# documentation and/or other materials provided with the distribution.
# * Neither the name of Calxeda Inc. nor the names of its contributors
# may be used to endorse or promote products derived from this software
# without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
# TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
# THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
# DAMAGE.

from cxmanage_api.cli import get_nodes, get_tftp, run_command, prompt_yes

def eepromupdate_command(args):
    """Updates the EEPROM's on a cluster or host"""
    def validate_config():
        """Makes sure the system type is applicable to EEPROM updates"""
        for node in  nodes:
            if('Dual Node' not in node.get_versions().hardware_version):
                print 'ERROR: eepromupdate is only valid on TerraNova systems'
                return True

        return False

    def validate_images():
        """Makes sure all the necessary images have been provided"""
        if(args.eeprom_location == 'node'):
            for node in nodes:
                node_hw_ver = node.get_versions().hardware_version
                if('Uplink' in node_hw_ver):
                    image = 'dual_uplink_node_%s' % (node.node_id % 4)
                else:
                    image = 'dual_node_%s' % (node.node_id % 4)
                if(not [img for img in args.images if image in img]):
                    print 'ERROR: no valid image for node %s' % node.node_id
                    return True

        else:
            image = args.images[0]
            if('tn_storage.single_slot' not in image):
                print 'ERROR: %s is an invalid image for slot EEPROM' % image
                return True

        return False

    def do_update():
        """Updates the EEPROM images"""
        if(args.eeprom_location == 'node'):
            for node in nodes:
                node_hw_ver = node.get_versions().hardware_version
                if('Uplink' in node_hw_ver):
                    needed_image = 'dual_uplink_node_%s' % (node.node_id % 4)
                else:
                    needed_image = 'dual_node_%s' % (node.node_id % 4)
                image = [img for img in args.images if needed_image in img][0]
                print 'Updating node EEPROM on node %s' % node.node_id
                if(args.verbose):
                    print '    %s' % image
                try:
                    node.update_node_eeprom(image)
                except Exception as err:
                    print 'ERROR: %s' % str(err)
                    return True

            print ''  # for readability
        else:
            image = args.images[0]
            # First node in every slot gets the slot image
            slot_nodes = [node for node in nodes if node.node_id % 4 == 0]
            _, errors = run_command(
                args, slot_nodes, "update_slot_eeprom", image
            )
            if(errors):
                print 'ERROR: EEPROM update failed'
                return True

        return False

    if not args.all_nodes:
        if args.force:
            print(
                'WARNING: Updating EEPROM without --all-nodes' +
                ' is dangerous.'
            )
        else:
            if not prompt_yes(
                'WARNING: Updating EEPROM without ' +
                '--all-nodes is dangerous. Continue?'
                ):
                return 1

    tftp = get_tftp(args)
    nodes = get_nodes(args, tftp, verify_prompt=True)

    errors = validate_config()

    if(not errors):
        errors = validate_images()

    if(not errors):
        errors = do_update()

    if not args.quiet and not errors:
        print "Command completed successfully."
        print "A power cycle is required for the update to take effect.\n"

    return errors