|
| 1 | +// Copyright 2019 The OpenSDS Authors. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +// not use this file except in compliance with the License. You may obtain |
| 5 | +// a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | +// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | +// License for the specific language governing permissions and limitations |
| 13 | +// under the License. |
| 14 | + |
| 15 | +package spectrumscale |
| 16 | + |
| 17 | +import ( |
| 18 | + "strconv" |
| 19 | + "strings" |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/appleboy/easyssh-proxy" |
| 23 | + "github.com/golang/glog" |
| 24 | + "github.com/opensds/opensds/pkg/utils/exec" |
| 25 | +) |
| 26 | + |
| 27 | +type MakeConfig struct { |
| 28 | + User string |
| 29 | + Server string |
| 30 | + Password string |
| 31 | + Port string |
| 32 | + Timeout time.Duration |
| 33 | +} |
| 34 | + |
| 35 | +func Executer() *easyssh.MakeConfig { |
| 36 | + ssh := &easyssh.MakeConfig{ |
| 37 | + User: username, |
| 38 | + Server: defaultTgtBindIp, |
| 39 | + Password: password, |
| 40 | + Port: port, |
| 41 | + Timeout: timeoutForssh * time.Second, |
| 42 | + } |
| 43 | + return ssh |
| 44 | +} |
| 45 | + |
| 46 | +type Cli struct { |
| 47 | + // Command executer |
| 48 | + BaseExecuter exec.Executer |
| 49 | + // Command Root executer |
| 50 | + RootExecuter exec.Executer |
| 51 | +} |
| 52 | + |
| 53 | +func login() error { |
| 54 | + stdout, stderr, done, err := Executer().Run("uname", timeoutForssh*time.Second) |
| 55 | + if err != nil { |
| 56 | + glog.Errorf("unable to establish connection, stderr:%v", stderr) |
| 57 | + return err |
| 58 | + } |
| 59 | + glog.Infof("connection established. stdout:%v done:%v", stdout, done) |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +func NewCli() (*Cli, error) { |
| 64 | + return &Cli{ |
| 65 | + BaseExecuter: exec.NewBaseExecuter(), |
| 66 | + RootExecuter: exec.NewRootExecuter(), |
| 67 | + }, nil |
| 68 | +} |
| 69 | + |
| 70 | +func (c *Cli) execute(cmd ...string) (string, error) { |
| 71 | + return c.RootExecuter.Run(cmd[0], cmd[1:]...) |
| 72 | +} |
| 73 | + |
| 74 | +// get the spectrumscale cluster status |
| 75 | +func (c *Cli) GetSpectrumScaleStatus() error { |
| 76 | + createCmd := "mmgetstate" |
| 77 | + stdout, stderr, done, err := Executer().Run(createCmd, timeoutForssh*time.Second) |
| 78 | + if err != nil { |
| 79 | + glog.Errorf("failed to execute command. stderr:%v", stderr) |
| 80 | + return err |
| 81 | + } |
| 82 | + // above command was successfull with some output |
| 83 | + glog.Infof("command execution was successful. stdout:%v done:%v", stdout, done) |
| 84 | + |
| 85 | + // now parse the output lines to get the status of spectrumscale cluster |
| 86 | + // the expected state is active |
| 87 | + lines := strings.Split(stdout, "\n") |
| 88 | + if !(strings.Contains(lines[2], "active")) { |
| 89 | + glog.Errorf("cluster state is not active") |
| 90 | + return err |
| 91 | + } |
| 92 | + glog.Infof("cluster state is active") |
| 93 | + return nil |
| 94 | +} |
| 95 | + |
| 96 | +// get spectrumscale mount point |
| 97 | +func (c *Cli) GetSpectrumScaleMountPoint() (string, string, error) { |
| 98 | + createCmd := "mmlsfs all -T" |
| 99 | + stdout, stderr, done, err := Executer().Run(createCmd, timeoutForssh*time.Second) |
| 100 | + if err != nil { |
| 101 | + glog.Errorf("failed to list all mountpoint. stderr:%v", stderr) |
| 102 | + return "", "", err |
| 103 | + } |
| 104 | + glog.Infof("the list of mountpoints: stdout:%v, done:%v", stdout, done) |
| 105 | + // now parse the output lines to get mountPoint |
| 106 | + // the example of mountPoint is /ibm/gpfs/fs1 |
| 107 | + var mountPoint string |
| 108 | + lines := strings.Split(stdout, "\n") |
| 109 | + for _, line := range lines { |
| 110 | + if strings.Contains(line, "-T") != true { |
| 111 | + continue |
| 112 | + } |
| 113 | + field := strings.Fields(line) |
| 114 | + mountPoint = field[1] |
| 115 | + } |
| 116 | + glog.Infof("the mountpoint is:%v", mountPoint) |
| 117 | + |
| 118 | + // now get the filesystem |
| 119 | + field := strings.Split(mountPoint, "/") |
| 120 | + length := len(field) |
| 121 | + filesystem := field[length-1] |
| 122 | + |
| 123 | + return mountPoint, filesystem, nil |
| 124 | +} |
| 125 | + |
| 126 | +// create volume |
| 127 | +func (c *Cli) CreateVolume(name string, size, filesystem, mountpoint string) error { |
| 128 | + createCmd := "mmcrfileset" + " " + "fs1" + " " + name + " " + "--inode-space" + " " + "new" |
| 129 | + stdout, stderr, done, err := Executer().Run(createCmd, timeoutForssh*time.Second) |
| 130 | + if err != nil { |
| 131 | + glog.Errorf("failed to create fileset. stderr:%v", stderr) |
| 132 | + return err |
| 133 | + } |
| 134 | + glog.Infof("fileset is successfully created. stdout:%v, done:%v", stdout, done) |
| 135 | + |
| 136 | + // now link the fileset with filesystem |
| 137 | + linkCmd := "mmlinkfileset" + " " + filesystem + " " + name + " " + "-J " + mountpoint + "/" + name |
| 138 | + stdout, stderr, done, err = Executer().Run(linkCmd, timeoutForssh*time.Second) |
| 139 | + if err != nil { |
| 140 | + glog.Errorf("failed to link fileset. stderr:%v", stderr) |
| 141 | + return err |
| 142 | + } |
| 143 | + glog.Infof("fileset is successfully linked. stdout:%v, done:%v", stdout, done) |
| 144 | + |
| 145 | + // now set the quota on fileset. Its nothing but allocating the size for fileset |
| 146 | + // for example: mmsetquota fs1:vol8 --block 1G:2G --files 10K:11K |
| 147 | + quotaCmd := "mmsetquota" + " " + "fs1" + ":" + name + " --block" + " " + size + "G" + ":" + size + "G" |
| 148 | + stdout, stderr, done, err = Executer().Run(quotaCmd, timeoutForssh*time.Second) |
| 149 | + if err != nil { |
| 150 | + glog.Errorf("failed to set the quota on fileset. stderr:%v", stderr) |
| 151 | + return err |
| 152 | + } |
| 153 | + glog.Infof("quota is successfully set on fileset. stdout:%v, done:%v", stdout, done) |
| 154 | + glog.Infof("volume:%v is successfuly created with size:%v", name, size) |
| 155 | + return err |
| 156 | +} |
| 157 | + |
| 158 | +// delete volume |
| 159 | +func (c *Cli) Delete(name string) error { |
| 160 | + unlinkCmd := "mmunlinkfileset" + " " + "fs1" + " " + name |
| 161 | + stdout, stderr, done, err := Executer().Run(unlinkCmd, timeoutForssh*time.Second) |
| 162 | + if err != nil { |
| 163 | + glog.Errorf("failed unlink the fileset. stderr:%v", stderr) |
| 164 | + return err |
| 165 | + } |
| 166 | + glog.Infof("filset unlinking successful.stdout:%v, done:%v", stdout, done) |
| 167 | + |
| 168 | + // once unlinking success, delete the fileset |
| 169 | + delCmd := "mmdelfileset" + " " + "fs1" + " " + name + " " + "-f" |
| 170 | + stdout, stderr, done, err = Executer().Run(delCmd, timeoutForssh*time.Second) |
| 171 | + if err != nil { |
| 172 | + glog.Errorf("failed delete the fileset. stderr:%v", stderr) |
| 173 | + return err |
| 174 | + } |
| 175 | + glog.Infof("fileset is successfully deleted. stdout:%v, done:%v", stdout, done) |
| 176 | + glog.Infof("volume:%v is successfuly deleted", name) |
| 177 | + return nil |
| 178 | +} |
| 179 | + |
| 180 | +// this is function for extending the volume size |
| 181 | +func (c *Cli) ExtendVolume(name string, newSize string) error { |
| 182 | + quotaCmd := "mmsetquota" + " " + "fs1" + ":" + name + " --block" + " " + newSize + "G" + ":" + newSize + "G" |
| 183 | + stdout, stderr, done, err := Executer().Run(quotaCmd, timeoutForssh*time.Second) |
| 184 | + if err != nil { |
| 185 | + glog.Errorf("failed extend the quota size on fileset. stderr:%v", stderr) |
| 186 | + return err |
| 187 | + } |
| 188 | + glog.Infof("quota is extended successfully. stdout:%v, done:%v", stdout, done) |
| 189 | + glog.Infof("volume:%v is extended successfully with newsize:%v", name, newSize) |
| 190 | + return nil |
| 191 | +} |
| 192 | + |
| 193 | +// this is function for creating the snapshot |
| 194 | +func (c *Cli) CreateSnapshot(snapName, volName string) error { |
| 195 | + cmd := "mmcrsnapshot" + " " + "fs1" + " " + snapName + " " + "-j" + " " + volName |
| 196 | + stdout, stderr, done, err := Executer().Run(cmd, timeoutForssh*time.Second) |
| 197 | + if err != nil { |
| 198 | + glog.Errorf("failed to create snapshot. stderr:%v", stderr) |
| 199 | + return err |
| 200 | + } |
| 201 | + glog.Infof("stdout:%v done:%v", stdout, done) |
| 202 | + glog.Infof("snapshot:%v is created successfully for volume:%v", snapName, volName) |
| 203 | + return nil |
| 204 | +} |
| 205 | + |
| 206 | +// this is function for deleting the snapshot |
| 207 | +func (c *Cli) DeleteSnapshot(volName, snapName string) error { |
| 208 | + cmd := "mmdelsnapshot" + " " + "fs1" + " " + volName + ":" + snapName |
| 209 | + stdout, stderr, done, err := Executer().Run(cmd, timeoutForssh*time.Second) |
| 210 | + glog.Infof("stdout:%v stderr:%v done:%v", stdout, stderr, done) |
| 211 | + if err != nil { |
| 212 | + glog.Errorf("failed to delete snapshot. stderr:%v", stderr) |
| 213 | + return err |
| 214 | + } |
| 215 | + glog.Infof("stdout:%v done:%v", stdout, done) |
| 216 | + glog.Infof("snapshot:%v is deleted successfully.", snapName) |
| 217 | + return nil |
| 218 | +} |
| 219 | + |
| 220 | +type Pools struct { |
| 221 | + Name string |
| 222 | + TotalCapacity int64 |
| 223 | + FreeCapacity int64 |
| 224 | + UUID string |
| 225 | +} |
| 226 | + |
| 227 | +// this function is for discover all the pool from spectrumscale cluster |
| 228 | +func (c *Cli) ListPools(mountPoint, filesystem string) (*[]Pools, error) { |
| 229 | + cmd := "mmlspool" + " " + filesystem |
| 230 | + stdout, stderr, done, err := Executer().Run(cmd, timeoutForssh*time.Second) |
| 231 | + glog.Infof("stdout:%v stderr:%v done:%v", stdout, stderr, done) |
| 232 | + if err != nil { |
| 233 | + glog.Errorf("failed to list all pools. stderr:%v", stderr) |
| 234 | + return nil, err |
| 235 | + } |
| 236 | + glog.Infof("the list of pools are: stdout:%v, done:%v", stdout, done) |
| 237 | + |
| 238 | + // now parse the lines to get all pools |
| 239 | + lines := strings.Split(stdout, "\n") |
| 240 | + var pols []Pools |
| 241 | + for _, line := range lines { |
| 242 | + if len(line) == 0 { |
| 243 | + continue |
| 244 | + } |
| 245 | + fields := strings.Fields(line) |
| 246 | + if fields[0] == "Storage" { |
| 247 | + continue |
| 248 | + } |
| 249 | + if fields[0] == "Name" { |
| 250 | + continue |
| 251 | + } |
| 252 | + |
| 253 | + total, _ := strconv.ParseFloat(fields[6], 64) |
| 254 | + free, _ := strconv.ParseFloat(fields[7], 64) |
| 255 | + pool := Pools{ |
| 256 | + Name: fields[0], |
| 257 | + TotalCapacity: int64(total / 1000000), |
| 258 | + FreeCapacity: int64(free / 1000000), |
| 259 | + UUID: fields[1], |
| 260 | + } |
| 261 | + pols = append(pols, pool) |
| 262 | + } |
| 263 | + return &pols, nil |
| 264 | +} |
0 commit comments