1# CDDL HEADER START
2#
3# The contents of this file are subject to the terms of the
4# Common Development and Distribution License (the "License").
5# You may not use this file except in compliance with the License.
6#
7# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
8# or http://www.opensolaris.org/os/licensing.
9# See the License for the specific language governing permissions
10# and limitations under the License.
11#
12# When distributing Covered Code, include this CDDL HEADER in each
13# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
14# If applicable, add the following below this CDDL HEADER, with the
15# fields enclosed by brackets "[]" replaced with your own identifying
16# information: Portions Copyright [yyyy] [name of copyright owner]
17#
18# CDDL HEADER END
19#
20
21#
22# Copyright (c) 2008, 2010, Oracle and/or its affiliates. All rights reserved.
23#
24
25"""
26beadm - The Boot Environment Administration tool.
27
28A module containing all of the messages output by beadm.
29"""
30
31import sys
32from beadm import _
33
34class Msgs:
35    """Indices corresponding to message numbers for beadm."""
36
37    (BEADM_ERR_ACTIVATE,
38    BEADM_ERR_BE_EXISTS,
39    BEADM_ERR_SNAP_EXISTS,
40    BEADM_ERR_CREATE,
41    BEADM_ERR_DESTROY,
42    BEADM_ERR_DESTROY_ACTIVE,
43    BEADM_ERR_BE_DOES_NOT_EXIST,
44    BEADM_ERR_NO_BES_EXIST,
45    BEADM_ERR_MSG_SUB,
46    BEADM_ERR_ILL_SUBCOMMAND,
47    BEADM_ERR_INVALID_RESPONSE,
48    BEADM_ERR_LIST,
49    BEADM_ERR_LIST_DATA,
50    BEADM_ERR_LOG_CREATE,
51    BEADM_ERR_LOG_RM,
52    BEADM_ERR_MOUNT,
53    BEADM_ERR_MOUNT_EXISTS,
54    BEADM_ERR_MOUNTED,
55    BEADM_ERR_MOUNTPOINT,
56    BEADM_ERR_MUTUALLY_EXCL,
57    BEADM_ERR_NO_MSG,
58    BEADM_ERR_NO_ZPOOL,
59    BEADM_ERR_NOT_SUPPORTED_NGZ,
60    BEADM_ERR_OPT_ARGS,
61    BEADM_ERR_OS,
62    BEADM_ERR_PERMISSIONS,
63    BEADM_ERR_RENAME,
64    BEADM_ERR_SHARED_FS,
65    BEADM_ERR_SNAP_DOES_NOT_EXISTS,
66    BEADM_ERR_UNMOUNT,
67    BEADM_ERR_UNMOUNT_ACTIVE,
68    BEADM_ERR_BENAME,
69    BEADM_MSG_ACTIVE_ON_BOOT,
70    BEADM_MSG_DESTROY,
71    BEADM_MSG_DESTROY_NO,
72    BEADM_MSG_BE_CREATE_START,
73    BEADM_MSG_BE_CREATE_SUCCESS,
74    BEADM_MSG_FREE_FORMAT,
75    ) = range(38)
76
77    # Indices corresponding to message numbers for libbe that we are
78    # interested in expanding messages.
79    (BE_ERR_ACCESS,
80    BE_ERR_ACTIVATE_CURR,
81    BE_ERR_AUTONAME,
82    BE_ERR_BE_NOENT,
83    BE_ERR_BUSY,
84    BE_ERR_CANCELED,
85    BE_ERR_CLONE,
86    BE_ERR_COPY,
87    BE_ERR_CREATDS,
88    BE_ERR_CURR_BE_NOT_FOUND,
89    BE_ERR_DESTROY,
90    BE_ERR_DEMOTE,
91    BE_ERR_DSTYPE,
92    BE_ERR_BE_EXISTS,
93    BE_ERR_INIT,
94    BE_ERR_INTR,
95    BE_ERR_INVAL,
96    BE_ERR_INVALPROP,
97    BE_ERR_INVALMOUNTPOINT,
98    BE_ERR_MOUNT,
99    BE_ERR_MOUNTED,
100    BE_ERR_NAMETOOLONG,
101    BE_ERR_NOENT,
102    BE_ERR_POOL_NOENT,
103    BE_ERR_NODEV,
104    BE_ERR_NOTMOUNTED,
105    BE_ERR_NOMEM,
106    BE_ERR_NONINHERIT,
107    BE_ERR_NXIO,
108    BE_ERR_NOSPC,
109    BE_ERR_NOTSUP,
110    BE_ERR_OPEN,
111    BE_ERR_PERM,
112    BE_ERR_UNAVAIL,
113    BE_ERR_PROMOTE,
114    BE_ERR_ROFS,
115    BE_ERR_READONLYDS,
116    BE_ERR_READONLYPROP,
117    BE_ERR_SS_EXISTS,
118    BE_ERR_SS_NOENT,
119    BE_ERR_UMOUNT,
120    BE_ERR_UMOUNT_CURR_BE,
121    BE_ERR_UMOUNT_SHARED,
122    BE_ERR_UNKNOWN,
123    BE_ERR_ZFS,
124    BE_ERR_DESTROY_CURR_BE,
125    BE_ERR_GEN_UUID,
126    BE_ERR_PARSE_UUID,
127    BE_ERR_NO_UUID,
128    BE_ERR_ZONE_NO_PARENTBE,
129    BE_ERR_ZONE_MULTIPLE_ACTIVE,
130    BE_ERR_ZONE_NO_ACTIVE_ROOT,
131    BE_ERR_ZONE_ROOT_NOT_LEGACY,
132    BE_ERR_NO_MOUNTED_ZONE,
133    BE_ERR_MOUNT_ZONEROOT,
134    BE_ERR_UMOUNT_ZONEROOT,
135    BE_ERR_ZONES_UNMOUNT,
136    BE_ERR_FAULT,
137    BE_ERR_RENAME_ACTIVE,
138    BE_ERR_NO_MENU,
139    BE_ERR_DEV_BUSY,
140    BE_ERR_BAD_MENU_PATH,
141    BE_ERR_ZONE_SS_EXISTS
142    ) = range(4000, 4063)
143
144    # Error message dictionaries.
145    mBeadmErr = {}
146    mBeadmOut = {}
147    mBeadmLog = {}
148
149    # Errors from beadm (to stderr).
150    mBeadmErr[BEADM_ERR_ACTIVATE] = _("Unable to activate %(0)s.\n%(1)s")
151    mBeadmErr[BEADM_ERR_BE_EXISTS] = _("BE %s already exists. Please choose a different BE name.")
152    mBeadmErr[BEADM_ERR_BE_DOES_NOT_EXIST] = _("%s does not exist or appear to be a valid BE.\nPlease check that the name of the BE provided is correct.")
153    mBeadmErr[BEADM_ERR_NO_BES_EXIST] = _("No boot environments found on this system.")
154    mBeadmErr[BEADM_ERR_CREATE] = _("Unable to create %(0)s.\n%(1)s")
155    mBeadmErr[BEADM_ERR_DESTROY] = _("Unable to destroy %(0)s.\n%(1)s")
156    mBeadmErr[BEADM_ERR_DESTROY_ACTIVE] = _("%(0)s is the currently active BE and cannot be destroyed.\nYou must boot from another BE in order to destroy %(1)s.")
157    mBeadmErr[BEADM_ERR_MSG_SUB] = _("Fatal error. No message associated with index %d")
158    mBeadmErr[BEADM_ERR_ILL_SUBCOMMAND] = _("Illegal subcommand %s")
159    mBeadmErr[BEADM_ERR_INVALID_RESPONSE] = _("Invalid response. Please enter 'y' or 'n'.")
160    mBeadmErr[BEADM_ERR_LIST] = _("Unable to display Boot Environment: %s")
161    mBeadmErr[BEADM_ERR_LIST_DATA] = _("Unable to process list data.")
162    mBeadmErr[BEADM_ERR_LOG_CREATE] = _("Unable to create log file.")
163    mBeadmErr[BEADM_ERR_LOG_RM] = _("Unable to remove %s")
164    mBeadmErr[BEADM_ERR_MOUNT] = _("Unable to mount %(0)s.\n%(1)s")
165    mBeadmErr[BEADM_ERR_MOUNT_EXISTS] = _("%s is already mounted.\nPlease unmount the BE before mounting it again.")
166    mBeadmErr[BEADM_ERR_MOUNTED] = _("Unable to destroy %(0)s.\nIt is currently mounted and must be unmounted before it can be destroyed.\nUse 'beadm unmount %(1)s' to unmount the BE before destroying\nit or 'beadm destroy -fF %(2)s'.")
167    mBeadmErr[BEADM_ERR_MOUNTPOINT] = _("Invalid mount point %s. Mount point must start with a /.")
168    mBeadmErr[BEADM_ERR_MUTUALLY_EXCL] = _("Invalid options: %s are mutually exclusive.")
169    mBeadmErr[BEADM_ERR_NO_MSG] = _("Unable to find message for error code: %d")
170    mBeadmErr[BEADM_ERR_NO_ZPOOL] = _("BE: %s was not found in any pool.\n The pool may not exist or the name of the BE is not correct.")
171    mBeadmErr[BEADM_ERR_NOT_SUPPORTED_NGZ] = _("beadm is not supported in a non-global zone.")
172    mBeadmErr[BEADM_ERR_OPT_ARGS] = _("Invalid options and arguments:")
173    mBeadmErr[BEADM_ERR_OS] = _("System error: %s")
174    mBeadmErr[BEADM_ERR_RENAME] = _("Rename of BE %(0)s failed.\n%(1)s")
175    mBeadmErr[BEADM_ERR_SHARED_FS] = _("%s is a shared file system and it cannot be unmounted.")
176    mBeadmErr[BEADM_ERR_SNAP_DOES_NOT_EXISTS] = _("%s does not exist or appear to be a valid snapshot.\nPlease check that the name of the snapshot provided is correct.")
177    mBeadmErr[BEADM_ERR_SNAP_EXISTS] = _("Snapshot %s already exists.\n Please choose a different snapshot name.")
178    mBeadmErr[BEADM_ERR_UNMOUNT] = _("Unable to unmount %(0)s.\n%(1)s")
179    mBeadmErr[BEADM_ERR_UNMOUNT_ACTIVE] = _("%s is the currently active BE.\nIt cannot be unmounted unless another BE is the currently active BE.")
180    mBeadmErr[BE_ERR_ZONES_UNMOUNT] = _("Unable to destroy one of %(0)s's zone BE's.\nUse 'beadm destroy -fF %(1)s' or 'zfs -f destroy <dataset>'.")
181    mBeadmErr[BEADM_ERR_PERMISSIONS] = _("You have insufficient privileges to execute this command.\nEither use 'pfexec' to execute the command or become superuser.")
182    mBeadmErr[BEADM_ERR_BENAME] = _("The BE name provided is invalid.\n Please check it and try again.")
183
184    # Catchall
185    mBeadmErr[BEADM_MSG_FREE_FORMAT] = "%s"
186
187    # Messages from beadm (to stdout).
188    mBeadmOut[BEADM_MSG_ACTIVE_ON_BOOT] = _("The BE that was just destroyed was the 'active on boot' BE.\n%s is now the 'active on boot' BE. Use 'beadm activate' to change it.\n")
189    mBeadmOut[BEADM_MSG_DESTROY] = _("Are you sure you want to destroy %s?  This action cannot be undone(y/[n]):")
190    mBeadmOut[BEADM_MSG_DESTROY_NO] = _("%s has not been destroyed.\n")
191
192    # Messages from beadm (to log only).
193    mBeadmLog[BEADM_MSG_BE_CREATE_START] = "Attempting to create %s"
194    mBeadmLog[BEADM_MSG_BE_CREATE_SUCCESS] = "%s was created successfully"
195
196msgLog, msgOut, msgErr = range(3)
197
198def printLog(string, log_fd):
199    """Print log."""
200
201    sendMsg(string, msgLog, log_fd)
202
203def printStdout(string, log_fd):
204    """Print standard output."""
205
206    sendMsg(string, msgOut, log_fd)
207
208def printStderr(string, log_fd):
209    """Print standard error."""
210
211    sendMsg(string, msgErr, log_fd)
212
213def composeMsg(string, txt=None):
214    """
215    Compose the message to be dispayed.
216    txt can be either a list or string object.
217    Return the newly composed string.
218    """
219
220    try:
221        msg = string % txt
222    except TypeError:
223        msg = string
224
225    return (msg)
226
227def sendMsg(string, mode, log_fd=-1):
228    """Send message."""
229
230    if mode == msgOut:
231        print >> sys.stdout, string,
232    if mode == msgErr:
233        print >> sys.stderr, string
234    if log_fd != -1 or mode == msgLog:
235        log_fd.write(string + "\n")
236
237def printMsg(msg_idx=-1, txt="", log_fd=-1):
238    """Print the message based on the message index."""
239
240    if msg_idx in Msgs.mBeadmErr:
241        printStderr(composeMsg(Msgs.mBeadmErr[msg_idx], txt),
242            log_fd)
243    elif msg_idx in Msgs.mBeadmOut:
244        printStdout(composeMsg(Msgs.mBeadmOut[msg_idx], txt),
245            log_fd)
246    elif msg_idx in Msgs.mBeadmLog:
247        printLog(composeMsg(Msgs.mBeadmLog[msg_idx], txt), log_fd)
248    else:
249        printStderr(composeMsg(Msgs.mLibbe[BEADM_ERR_MSG_SUB],
250            msg_idx), -1)
251        sys.exit(1)
252
253def getMsg(msg_idx=-1, txt=""):
254    """Print the message based on the message index."""
255
256    if msg_idx in  Msgs.mBeadmErr:
257        return(composeMsg(Msgs.mBeadmErr[msg_idx], txt))
258    elif msg_idx in Msgs.mBeadmOut:
259        return(composeMsg(Msgs.mBeadmOut[msg_idx], txt))
260    elif msg_idx in Msgs.mBeadmLog:
261        return(composeMsg(Msgs.mBeadmLog[msg_idx], txt))
262    else:
263        return(composeMsg(Msgs.mLibbe[BEADM_ERR_MSG_SUB]))
264        sys.exit(1)
265