You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

1615 lines
45 KiB

  1. /*
  2. Copyright 2015 Google LLC
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package main
  14. // Command docs are in cbtdoc.go.
  15. import (
  16. "bytes"
  17. "context"
  18. "encoding/csv"
  19. "flag"
  20. "fmt"
  21. "go/format"
  22. "io"
  23. "log"
  24. "os"
  25. "regexp"
  26. "sort"
  27. "strconv"
  28. "strings"
  29. "text/tabwriter"
  30. "text/template"
  31. "time"
  32. "cloud.google.com/go/bigtable"
  33. "cloud.google.com/go/bigtable/internal/cbtconfig"
  34. "google.golang.org/api/iterator"
  35. "google.golang.org/api/option"
  36. "google.golang.org/grpc"
  37. )
  38. var (
  39. oFlag = flag.String("o", "", "if set, redirect stdout to this file")
  40. config *cbtconfig.Config
  41. client *bigtable.Client
  42. adminClient *bigtable.AdminClient
  43. instanceAdminClient *bigtable.InstanceAdminClient
  44. version = "<unknown version>"
  45. revision = "<unknown revision>"
  46. revisionDate = "<unknown revision date>"
  47. cliUserAgent = "cbt-cli-go/unknown"
  48. )
  49. func getCredentialOpts(opts []option.ClientOption) []option.ClientOption {
  50. if ts := config.TokenSource; ts != nil {
  51. opts = append(opts, option.WithTokenSource(ts))
  52. }
  53. if tlsCreds := config.TLSCreds; tlsCreds != nil {
  54. opts = append(opts, option.WithGRPCDialOption(grpc.WithTransportCredentials(tlsCreds)))
  55. }
  56. return opts
  57. }
  58. func getClient(clientConf bigtable.ClientConfig) *bigtable.Client {
  59. if client == nil {
  60. var opts []option.ClientOption
  61. if ep := config.DataEndpoint; ep != "" {
  62. opts = append(opts, option.WithEndpoint(ep))
  63. }
  64. opts = append(opts, option.WithUserAgent(cliUserAgent))
  65. opts = getCredentialOpts(opts)
  66. var err error
  67. client, err = bigtable.NewClientWithConfig(context.Background(), config.Project, config.Instance, clientConf, opts...)
  68. if err != nil {
  69. log.Fatalf("Making bigtable.Client: %v", err)
  70. }
  71. }
  72. return client
  73. }
  74. func getAdminClient() *bigtable.AdminClient {
  75. if adminClient == nil {
  76. var opts []option.ClientOption
  77. if ep := config.AdminEndpoint; ep != "" {
  78. opts = append(opts, option.WithEndpoint(ep))
  79. }
  80. opts = append(opts, option.WithUserAgent(cliUserAgent))
  81. opts = getCredentialOpts(opts)
  82. var err error
  83. adminClient, err = bigtable.NewAdminClient(context.Background(), config.Project, config.Instance, opts...)
  84. if err != nil {
  85. log.Fatalf("Making bigtable.AdminClient: %v", err)
  86. }
  87. }
  88. return adminClient
  89. }
  90. func getInstanceAdminClient() *bigtable.InstanceAdminClient {
  91. if instanceAdminClient == nil {
  92. var opts []option.ClientOption
  93. if ep := config.AdminEndpoint; ep != "" {
  94. opts = append(opts, option.WithEndpoint(ep))
  95. }
  96. opts = getCredentialOpts(opts)
  97. var err error
  98. instanceAdminClient, err = bigtable.NewInstanceAdminClient(context.Background(), config.Project, opts...)
  99. if err != nil {
  100. log.Fatalf("Making bigtable.InstanceAdminClient: %v", err)
  101. }
  102. }
  103. return instanceAdminClient
  104. }
  105. func main() {
  106. var err error
  107. config, err = cbtconfig.Load()
  108. if err != nil {
  109. log.Fatal(err)
  110. }
  111. config.RegisterFlags()
  112. flag.Usage = func() { usage(os.Stderr) }
  113. flag.Parse()
  114. if flag.NArg() == 0 {
  115. usage(os.Stderr)
  116. os.Exit(1)
  117. }
  118. if *oFlag != "" {
  119. f, err := os.Create(*oFlag)
  120. if err != nil {
  121. log.Fatal(err)
  122. }
  123. defer func() {
  124. if err := f.Close(); err != nil {
  125. log.Fatal(err)
  126. }
  127. }()
  128. os.Stdout = f
  129. }
  130. if config.UserAgent != "" {
  131. cliUserAgent = config.UserAgent
  132. }
  133. ctx := context.Background()
  134. for _, cmd := range commands {
  135. if cmd.Name == flag.Arg(0) {
  136. if err := config.CheckFlags(cmd.Required); err != nil {
  137. log.Fatal(err)
  138. }
  139. cmd.do(ctx, flag.Args()[1:]...)
  140. return
  141. }
  142. }
  143. log.Fatalf("Unknown command %q", flag.Arg(0))
  144. }
  145. func usage(w io.Writer) {
  146. fmt.Fprintf(w, "Usage: %s [flags] <command> ...\n", os.Args[0])
  147. flag.CommandLine.SetOutput(w)
  148. flag.CommandLine.PrintDefaults()
  149. fmt.Fprintf(w, "\n%s", cmdSummary)
  150. }
  151. var cmdSummary string // generated in init, below
  152. func init() {
  153. var buf bytes.Buffer
  154. tw := tabwriter.NewWriter(&buf, 10, 8, 4, '\t', 0)
  155. for _, cmd := range commands {
  156. fmt.Fprintf(tw, "cbt %s\t%s\n", cmd.Name, cmd.Desc)
  157. }
  158. tw.Flush()
  159. buf.WriteString(configHelp)
  160. buf.WriteString("\ncbt " + version + " " + revision + " " + revisionDate + "\n")
  161. cmdSummary = buf.String()
  162. }
  163. var configHelp = `
  164. Alpha features are not currently available to most Cloud Bigtable customers. The
  165. features might be changed in backward-incompatible ways and are not recommended
  166. for production use. They are not subject to any SLA or deprecation policy.
  167. Note: cbt does not support specifying arbitrary bytes on the command line for
  168. any value that Cloud Bigtable otherwise supports (for example, the row key and
  169. column qualifier).
  170. For convenience, values of the -project, -instance, -creds,
  171. -admin-endpoint and -data-endpoint flags may be specified in
  172. ~/.cbtrc in this format:
  173. project = my-project-123
  174. instance = my-instance
  175. creds = path-to-account-key.json
  176. admin-endpoint = hostname:port
  177. data-endpoint = hostname:port
  178. All values are optional, and all will be overridden by flags.
  179. `
  180. var commands = []struct {
  181. Name, Desc string
  182. do func(context.Context, ...string)
  183. Usage string
  184. Required cbtconfig.RequiredFlags
  185. }{
  186. {
  187. Name: "count",
  188. Desc: "Count rows in a table",
  189. do: doCount,
  190. Usage: "cbt count <table>",
  191. Required: cbtconfig.ProjectAndInstanceRequired,
  192. },
  193. {
  194. Name: "createinstance",
  195. Desc: "Create an instance with an initial cluster",
  196. do: doCreateInstance,
  197. Usage: "cbt createinstance <instance-id> <display-name> <cluster-id> <zone> <num-nodes> <storage type>\n" +
  198. " instance-id Permanent, unique id for the instance\n" +
  199. " display-name Description of the instance\n" +
  200. " cluster-id Permanent, unique id for the cluster in the instance\n" +
  201. " zone The zone in which to create the cluster\n" +
  202. " num-nodes The number of nodes to create\n" +
  203. " storage-type SSD or HDD\n",
  204. Required: cbtconfig.ProjectRequired,
  205. },
  206. {
  207. Name: "createcluster",
  208. Desc: "Create a cluster in the configured instance ",
  209. do: doCreateCluster,
  210. Usage: "cbt createcluster <cluster-id> <zone> <num-nodes> <storage type>\n" +
  211. " cluster-id Permanent, unique id for the cluster in the instance\n" +
  212. " zone The zone in which to create the cluster\n" +
  213. " num-nodes The number of nodes to create\n" +
  214. " storage-type SSD or HDD\n",
  215. Required: cbtconfig.ProjectAndInstanceRequired,
  216. },
  217. {
  218. Name: "createfamily",
  219. Desc: "Create a column family",
  220. do: doCreateFamily,
  221. Usage: "cbt createfamily <table> <family>",
  222. Required: cbtconfig.ProjectAndInstanceRequired,
  223. },
  224. {
  225. Name: "createtable",
  226. Desc: "Create a table",
  227. do: doCreateTable,
  228. Usage: "cbt createtable <table> [families=family[:gcpolicy],...] [splits=split,...]\n" +
  229. " families: Column families and their associated GC policies. For gcpolicy,\n" +
  230. " see \"setgcpolicy\".\n" +
  231. " Example: families=family1:maxage=1w,family2:maxversions=1\n" +
  232. " splits: Row key to be used to initially split the table",
  233. Required: cbtconfig.ProjectAndInstanceRequired,
  234. },
  235. {
  236. Name: "updatecluster",
  237. Desc: "Update a cluster in the configured instance",
  238. do: doUpdateCluster,
  239. Usage: "cbt updatecluster <cluster-id> [num-nodes=num-nodes]\n" +
  240. " cluster-id Permanent, unique id for the cluster in the instance\n" +
  241. " num-nodes The number of nodes to update to",
  242. Required: cbtconfig.ProjectAndInstanceRequired,
  243. },
  244. {
  245. Name: "deleteinstance",
  246. Desc: "Delete an instance",
  247. do: doDeleteInstance,
  248. Usage: "cbt deleteinstance <instance>",
  249. Required: cbtconfig.ProjectRequired,
  250. },
  251. {
  252. Name: "deletecluster",
  253. Desc: "Delete a cluster from the configured instance ",
  254. do: doDeleteCluster,
  255. Usage: "cbt deletecluster <cluster>",
  256. Required: cbtconfig.ProjectAndInstanceRequired,
  257. },
  258. {
  259. Name: "deletecolumn",
  260. Desc: "Delete all cells in a column",
  261. do: doDeleteColumn,
  262. Usage: "cbt deletecolumn <table> <row> <family> <column> [app-profile=<app profile id>]\n" +
  263. " app-profile=<app profile id> The app profile id to use for the request\n",
  264. Required: cbtconfig.ProjectAndInstanceRequired,
  265. },
  266. {
  267. Name: "deletefamily",
  268. Desc: "Delete a column family",
  269. do: doDeleteFamily,
  270. Usage: "cbt deletefamily <table> <family>",
  271. Required: cbtconfig.ProjectAndInstanceRequired,
  272. },
  273. {
  274. Name: "deleterow",
  275. Desc: "Delete a row",
  276. do: doDeleteRow,
  277. Usage: "cbt deleterow <table> <row> [app-profile=<app profile id>]\n" +
  278. " app-profile=<app profile id> The app profile id to use for the request\n",
  279. Required: cbtconfig.ProjectAndInstanceRequired,
  280. },
  281. {
  282. Name: "deletetable",
  283. Desc: "Delete a table",
  284. do: doDeleteTable,
  285. Usage: "cbt deletetable <table>",
  286. Required: cbtconfig.ProjectAndInstanceRequired,
  287. },
  288. {
  289. Name: "doc",
  290. Desc: "Print godoc-suitable documentation for cbt",
  291. do: doDoc,
  292. Usage: "cbt doc",
  293. Required: cbtconfig.NoneRequired,
  294. },
  295. {
  296. Name: "help",
  297. Desc: "Print help text",
  298. do: doHelp,
  299. Usage: "cbt help [command]",
  300. Required: cbtconfig.NoneRequired,
  301. },
  302. {
  303. Name: "listinstances",
  304. Desc: "List instances in a project",
  305. do: doListInstances,
  306. Usage: "cbt listinstances",
  307. Required: cbtconfig.ProjectRequired,
  308. },
  309. {
  310. Name: "listclusters",
  311. Desc: "List clusters in an instance",
  312. do: doListClusters,
  313. Usage: "cbt listclusters",
  314. Required: cbtconfig.ProjectAndInstanceRequired,
  315. },
  316. {
  317. Name: "lookup",
  318. Desc: "Read from a single row",
  319. do: doLookup,
  320. Usage: "cbt lookup <table> <row> [columns=[family]:[qualifier],...] [cells-per-column=<n>] " +
  321. "[app-profile=<app profile id>]\n" +
  322. " columns=[family]:[qualifier],... Read only these columns, comma-separated\n" +
  323. " cells-per-column=<n> Read only this many cells per column\n" +
  324. " app-profile=<app profile id> The app profile id to use for the request\n",
  325. Required: cbtconfig.ProjectAndInstanceRequired,
  326. },
  327. {
  328. Name: "ls",
  329. Desc: "List tables and column families",
  330. do: doLS,
  331. Usage: "cbt ls List tables\n" +
  332. "cbt ls <table> List column families in <table>",
  333. Required: cbtconfig.ProjectAndInstanceRequired,
  334. },
  335. {
  336. Name: "mddoc",
  337. Desc: "Print documentation for cbt in Markdown format",
  338. do: doMDDoc,
  339. Usage: "cbt mddoc",
  340. Required: cbtconfig.NoneRequired,
  341. },
  342. {
  343. Name: "read",
  344. Desc: "Read rows",
  345. do: doRead,
  346. Usage: "cbt read <table> [start=<row>] [end=<row>] [prefix=<prefix>]" +
  347. " [regex=<regex>] [columns=[family]:[qualifier],...] [count=<n>] [cells-per-column=<n>]" +
  348. " [app-profile=<app profile id>]\n" +
  349. " start=<row> Start reading at this row\n" +
  350. " end=<row> Stop reading before this row\n" +
  351. " prefix=<prefix> Read rows with this prefix\n" +
  352. " regex=<regex> Read rows with keys matching this regex\n" +
  353. " columns=[family]:[qualifier],... Read only these columns, comma-separated\n" +
  354. " count=<n> Read only this many rows\n" +
  355. " cells-per-column=<n> Read only this many cells per column\n" +
  356. " app-profile=<app profile id> The app profile id to use for the request\n",
  357. Required: cbtconfig.ProjectAndInstanceRequired,
  358. },
  359. {
  360. Name: "set",
  361. Desc: "Set value of a cell",
  362. do: doSet,
  363. Usage: "cbt set <table> <row> [app-profile=<app profile id>] family:column=val[@ts] ...\n" +
  364. " app-profile=<app profile id> The app profile id to use for the request\n" +
  365. " family:column=val[@ts] may be repeated to set multiple cells.\n" +
  366. "\n" +
  367. " ts is an optional integer timestamp.\n" +
  368. " If it cannot be parsed, the `@ts` part will be\n" +
  369. " interpreted as part of the value.",
  370. Required: cbtconfig.ProjectAndInstanceRequired,
  371. },
  372. {
  373. Name: "setgcpolicy",
  374. Desc: "Set the GC policy for a column family",
  375. do: doSetGCPolicy,
  376. Usage: "cbt setgcpolicy <table> <family> ((maxage=<d> | maxversions=<n>) [(and|or) (maxage=<d> | maxversions=<n>),...] | never)\n" +
  377. "\n" +
  378. ` maxage=<d> Maximum timestamp age to preserve (e.g. "1h", "4d")` + "\n" +
  379. " maxversions=<n> Maximum number of versions to preserve",
  380. Required: cbtconfig.ProjectAndInstanceRequired,
  381. },
  382. {
  383. Name: "waitforreplication",
  384. Desc: "Block until all the completed writes have been replicated to all the clusters",
  385. do: doWaitForReplicaiton,
  386. Usage: "cbt waitforreplication <table>",
  387. Required: cbtconfig.ProjectAndInstanceRequired,
  388. },
  389. {
  390. Name: "createtablefromsnapshot",
  391. Desc: "Create a table from a snapshot (snapshots alpha)",
  392. do: doCreateTableFromSnapshot,
  393. Usage: "cbt createtablefromsnapshot <table> <cluster> <snapshot>\n" +
  394. " table The name of the table to create\n" +
  395. " cluster The cluster where the snapshot is located\n" +
  396. " snapshot The snapshot to restore",
  397. Required: cbtconfig.ProjectAndInstanceRequired,
  398. },
  399. {
  400. Name: "createsnapshot",
  401. Desc: "Create a snapshot from a source table (snapshots alpha)",
  402. do: doSnapshotTable,
  403. Usage: "cbt createsnapshot <cluster> <snapshot> <table> [ttl=<d>]\n" +
  404. "\n" +
  405. ` [ttl=<d>] Lifespan of the snapshot (e.g. "1h", "4d")` + "\n",
  406. Required: cbtconfig.ProjectAndInstanceRequired,
  407. },
  408. {
  409. Name: "listsnapshots",
  410. Desc: "List snapshots in a cluster (snapshots alpha)",
  411. do: doListSnapshots,
  412. Usage: "cbt listsnapshots [<cluster>]",
  413. Required: cbtconfig.ProjectAndInstanceRequired,
  414. },
  415. {
  416. Name: "getsnapshot",
  417. Desc: "Get snapshot info (snapshots alpha)",
  418. do: doGetSnapshot,
  419. Usage: "cbt getsnapshot <cluster> <snapshot>",
  420. Required: cbtconfig.ProjectAndInstanceRequired,
  421. },
  422. {
  423. Name: "deletesnapshot",
  424. Desc: "Delete snapshot in a cluster (snapshots alpha)",
  425. do: doDeleteSnapshot,
  426. Usage: "cbt deletesnapshot <cluster> <snapshot>",
  427. Required: cbtconfig.ProjectAndInstanceRequired,
  428. },
  429. {
  430. Name: "version",
  431. Desc: "Print the current cbt version",
  432. do: doVersion,
  433. Usage: "cbt version",
  434. Required: cbtconfig.NoneRequired,
  435. },
  436. {
  437. Name: "createappprofile",
  438. Desc: "Creates app profile for an instance",
  439. do: doCreateAppProfile,
  440. Usage: "usage: cbt createappprofile <instance-id> <profile-id> <description> " +
  441. "(route-any | [ route-to=<cluster-id> : transactional-writes]) [optional flag] \n" +
  442. "optional flags may be `force`",
  443. Required: cbtconfig.ProjectAndInstanceRequired,
  444. },
  445. {
  446. Name: "getappprofile",
  447. Desc: "Reads app profile for an instance",
  448. do: doGetAppProfile,
  449. Usage: "cbt getappprofile <instance-id> <profile-id>",
  450. Required: cbtconfig.ProjectAndInstanceRequired,
  451. },
  452. {
  453. Name: "listappprofile",
  454. Desc: "Lists app profile for an instance",
  455. do: doListAppProfiles,
  456. Usage: "cbt listappprofile <instance-id> ",
  457. Required: cbtconfig.ProjectAndInstanceRequired,
  458. },
  459. {
  460. Name: "updateappprofile",
  461. Desc: "Updates app profile for an instance",
  462. do: doUpdateAppProfile,
  463. Usage: "usage: cbt updateappprofile <instance-id> <profile-id> <description>" +
  464. "(route-any | [ route-to=<cluster-id> : transactional-writes]) [optional flag] \n" +
  465. "optional flags may be `force`",
  466. Required: cbtconfig.ProjectAndInstanceRequired,
  467. },
  468. {
  469. Name: "deleteappprofile",
  470. Desc: "Deletes app profile for an instance",
  471. do: doDeleteAppProfile,
  472. Usage: "cbt deleteappprofile <instance-id> <profile-id>",
  473. Required: cbtconfig.ProjectAndInstanceRequired,
  474. },
  475. }
  476. func doCount(ctx context.Context, args ...string) {
  477. if len(args) != 1 {
  478. log.Fatal("usage: cbt count <table>")
  479. }
  480. tbl := getClient(bigtable.ClientConfig{}).Open(args[0])
  481. n := 0
  482. err := tbl.ReadRows(ctx, bigtable.InfiniteRange(""), func(_ bigtable.Row) bool {
  483. n++
  484. return true
  485. }, bigtable.RowFilter(bigtable.StripValueFilter()))
  486. if err != nil {
  487. log.Fatalf("Reading rows: %v", err)
  488. }
  489. fmt.Println(n)
  490. }
  491. func doCreateTable(ctx context.Context, args ...string) {
  492. if len(args) < 1 {
  493. log.Fatal("usage: cbt createtable <table> [families=family[:gcpolicy],...] [splits=split,...]")
  494. }
  495. tblConf := bigtable.TableConf{TableID: args[0]}
  496. parsed, err := parseArgs(args[1:], []string{"families", "splits"})
  497. if err != nil {
  498. log.Fatal(err)
  499. }
  500. for key, val := range parsed {
  501. chunks, err := csv.NewReader(strings.NewReader(val)).Read()
  502. if err != nil {
  503. log.Fatalf("Invalid %s arg format: %v", key, err)
  504. }
  505. switch key {
  506. case "families":
  507. tblConf.Families = make(map[string]bigtable.GCPolicy)
  508. for _, family := range chunks {
  509. famPolicy := strings.Split(family, ":")
  510. var gcPolicy bigtable.GCPolicy
  511. if len(famPolicy) < 2 {
  512. gcPolicy = bigtable.MaxVersionsPolicy(1)
  513. log.Printf("Using default GC Policy of %v for family %v", gcPolicy, family)
  514. } else {
  515. gcPolicy, err = parseGCPolicy(famPolicy[1])
  516. if err != nil {
  517. log.Fatal(err)
  518. }
  519. }
  520. tblConf.Families[famPolicy[0]] = gcPolicy
  521. }
  522. case "splits":
  523. tblConf.SplitKeys = chunks
  524. }
  525. }
  526. if err := getAdminClient().CreateTableFromConf(ctx, &tblConf); err != nil {
  527. log.Fatalf("Creating table: %v", err)
  528. }
  529. }
  530. func doCreateFamily(ctx context.Context, args ...string) {
  531. if len(args) != 2 {
  532. log.Fatal("usage: cbt createfamily <table> <family>")
  533. }
  534. err := getAdminClient().CreateColumnFamily(ctx, args[0], args[1])
  535. if err != nil {
  536. log.Fatalf("Creating column family: %v", err)
  537. }
  538. }
  539. func doCreateInstance(ctx context.Context, args ...string) {
  540. if len(args) < 6 {
  541. log.Fatal("cbt createinstance <instance-id> <display-name> <cluster-id> <zone> <num-nodes> <storage type>")
  542. }
  543. numNodes, err := strconv.ParseInt(args[4], 0, 32)
  544. if err != nil {
  545. log.Fatalf("Bad num-nodes %q: %v", args[4], err)
  546. }
  547. sType, err := parseStorageType(args[5])
  548. if err != nil {
  549. log.Fatal(err)
  550. }
  551. ic := bigtable.InstanceWithClustersConfig{
  552. InstanceID: args[0],
  553. DisplayName: args[1],
  554. Clusters: []bigtable.ClusterConfig{{
  555. ClusterID: args[2],
  556. Zone: args[3],
  557. NumNodes: int32(numNodes),
  558. StorageType: sType,
  559. }},
  560. }
  561. err = getInstanceAdminClient().CreateInstanceWithClusters(ctx, &ic)
  562. if err != nil {
  563. log.Fatalf("Creating instance: %v", err)
  564. }
  565. }
  566. func doCreateCluster(ctx context.Context, args ...string) {
  567. if len(args) < 4 {
  568. log.Fatal("usage: cbt createcluster <cluster-id> <zone> <num-nodes> <storage type>")
  569. }
  570. numNodes, err := strconv.ParseInt(args[2], 0, 32)
  571. if err != nil {
  572. log.Fatalf("Bad num_nodes %q: %v", args[2], err)
  573. }
  574. sType, err := parseStorageType(args[3])
  575. if err != nil {
  576. log.Fatal(err)
  577. }
  578. cc := bigtable.ClusterConfig{
  579. InstanceID: config.Instance,
  580. ClusterID: args[0],
  581. Zone: args[1],
  582. NumNodes: int32(numNodes),
  583. StorageType: sType,
  584. }
  585. err = getInstanceAdminClient().CreateCluster(ctx, &cc)
  586. if err != nil {
  587. log.Fatalf("Creating cluster: %v", err)
  588. }
  589. }
  590. func doUpdateCluster(ctx context.Context, args ...string) {
  591. if len(args) < 2 {
  592. log.Fatal("cbt updatecluster <cluster-id> [num-nodes=num-nodes]")
  593. }
  594. numNodes := int64(0)
  595. parsed, err := parseArgs(args[1:], []string{"num-nodes"})
  596. if err != nil {
  597. log.Fatal(err)
  598. }
  599. if val, ok := parsed["num-nodes"]; ok {
  600. numNodes, err = strconv.ParseInt(val, 0, 32)
  601. if err != nil {
  602. log.Fatalf("Bad num-nodes %q: %v", val, err)
  603. }
  604. }
  605. if numNodes > 0 {
  606. err = getInstanceAdminClient().UpdateCluster(ctx, config.Instance, args[0], int32(numNodes))
  607. if err != nil {
  608. log.Fatalf("Updating cluster: %v", err)
  609. }
  610. } else {
  611. log.Fatal("Updating cluster: nothing to update")
  612. }
  613. }
  614. func doDeleteInstance(ctx context.Context, args ...string) {
  615. if len(args) != 1 {
  616. log.Fatal("usage: cbt deleteinstance <instance>")
  617. }
  618. err := getInstanceAdminClient().DeleteInstance(ctx, args[0])
  619. if err != nil {
  620. log.Fatalf("Deleting instance: %v", err)
  621. }
  622. }
  623. func doDeleteCluster(ctx context.Context, args ...string) {
  624. if len(args) != 1 {
  625. log.Fatal("usage: cbt deletecluster <cluster>")
  626. }
  627. err := getInstanceAdminClient().DeleteCluster(ctx, config.Instance, args[0])
  628. if err != nil {
  629. log.Fatalf("Deleting cluster: %v", err)
  630. }
  631. }
  632. func doDeleteColumn(ctx context.Context, args ...string) {
  633. usage := "usage: cbt deletecolumn <table> <row> <family> <column> [app-profile=<app profile id>]"
  634. if len(args) != 4 && len(args) != 5 {
  635. log.Fatal(usage)
  636. }
  637. var appProfile string
  638. if len(args) == 5 {
  639. if !strings.HasPrefix(args[4], "app-profile=") {
  640. log.Fatal(usage)
  641. }
  642. appProfile = strings.Split(args[4], "=")[1]
  643. }
  644. tbl := getClient(bigtable.ClientConfig{AppProfile: appProfile}).Open(args[0])
  645. mut := bigtable.NewMutation()
  646. mut.DeleteCellsInColumn(args[2], args[3])
  647. if err := tbl.Apply(ctx, args[1], mut); err != nil {
  648. log.Fatalf("Deleting cells in column: %v", err)
  649. }
  650. }
  651. func doDeleteFamily(ctx context.Context, args ...string) {
  652. if len(args) != 2 {
  653. log.Fatal("usage: cbt deletefamily <table> <family>")
  654. }
  655. err := getAdminClient().DeleteColumnFamily(ctx, args[0], args[1])
  656. if err != nil {
  657. log.Fatalf("Deleting column family: %v", err)
  658. }
  659. }
  660. func doDeleteRow(ctx context.Context, args ...string) {
  661. usage := "usage: cbt deleterow <table> <row> [app-profile=<app profile id>]"
  662. if len(args) != 2 && len(args) != 3 {
  663. log.Fatal(usage)
  664. }
  665. var appProfile string
  666. if len(args) == 3 {
  667. if !strings.HasPrefix(args[2], "app-profile=") {
  668. log.Fatal(usage)
  669. }
  670. appProfile = strings.Split(args[2], "=")[1]
  671. }
  672. tbl := getClient(bigtable.ClientConfig{AppProfile: appProfile}).Open(args[0])
  673. mut := bigtable.NewMutation()
  674. mut.DeleteRow()
  675. if err := tbl.Apply(ctx, args[1], mut); err != nil {
  676. log.Fatalf("Deleting row: %v", err)
  677. }
  678. }
  679. func doDeleteTable(ctx context.Context, args ...string) {
  680. if len(args) != 1 {
  681. log.Fatalf("Can't do `cbt deletetable %s`", args)
  682. }
  683. err := getAdminClient().DeleteTable(ctx, args[0])
  684. if err != nil {
  685. log.Fatalf("Deleting table: %v", err)
  686. }
  687. }
  688. // to break circular dependencies
  689. var (
  690. doDocFn func(ctx context.Context, args ...string)
  691. doHelpFn func(ctx context.Context, args ...string)
  692. doMDDocFn func(ctx context.Context, args ...string)
  693. )
  694. func init() {
  695. doDocFn = doDocReal
  696. doHelpFn = doHelpReal
  697. doMDDocFn = doMDDocReal
  698. }
  699. func doDoc(ctx context.Context, args ...string) { doDocFn(ctx, args...) }
  700. func doHelp(ctx context.Context, args ...string) { doHelpFn(ctx, args...) }
  701. func doMDDoc(ctx context.Context, args ...string) { doMDDocFn(ctx, args...) }
  702. func docFlags() []*flag.Flag {
  703. // Only include specific flags, in a specific order.
  704. var flags []*flag.Flag
  705. for _, name := range []string{"project", "instance", "creds"} {
  706. f := flag.Lookup(name)
  707. if f == nil {
  708. log.Fatalf("Flag not linked: -%s", name)
  709. }
  710. flags = append(flags, f)
  711. }
  712. return flags
  713. }
  714. func doDocReal(ctx context.Context, args ...string) {
  715. data := map[string]interface{}{
  716. "Commands": commands,
  717. "Flags": docFlags(),
  718. "ConfigHelp": configHelp,
  719. }
  720. var buf bytes.Buffer
  721. if err := docTemplate.Execute(&buf, data); err != nil {
  722. log.Fatalf("Bad doc template: %v", err)
  723. }
  724. out, err := format.Source(buf.Bytes())
  725. if err != nil {
  726. log.Fatalf("Bad doc output: %v", err)
  727. }
  728. os.Stdout.Write(out)
  729. }
  730. func indentLines(s, ind string) string {
  731. ss := strings.Split(s, "\n")
  732. for i, p := range ss {
  733. ss[i] = ind + p
  734. }
  735. return strings.Join(ss, "\n")
  736. }
  737. var docTemplate = template.Must(template.New("doc").Funcs(template.FuncMap{
  738. "indent": indentLines,
  739. }).
  740. Parse(`
  741. // Copyright 2016 Google LLC
  742. //
  743. // Licensed under the Apache License, Version 2.0 (the "License");
  744. // you may not use this file except in compliance with the License.
  745. // You may obtain a copy of the License at
  746. //
  747. // http://www.apache.org/licenses/LICENSE-2.0
  748. //
  749. // Unless required by applicable law or agreed to in writing, software
  750. // distributed under the License is distributed on an "AS IS" BASIS,
  751. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  752. // See the License for the specific language governing permissions and
  753. // limitations under the License.
  754. // DO NOT EDIT. THIS IS AUTOMATICALLY GENERATED.
  755. // Run "go generate" to regenerate.
  756. //go:generate go run cbt.go gcpolicy.go -o cbtdoc.go doc
  757. /*
  758. Cbt is a tool for doing basic interactions with Cloud Bigtable. To learn how to
  759. install the cbt tool, see the
  760. [cbt overview](https://cloud.google.com/bigtable/docs/cbt-overview).
  761. Usage:
  762. cbt [options] command [arguments]
  763. The commands are:
  764. {{range .Commands}}
  765. {{printf "%-25s %s" .Name .Desc}}{{end}}
  766. Use "cbt help <command>" for more information about a command.
  767. The options are:
  768. {{range .Flags}}
  769. -{{.Name}} string
  770. {{.Usage}}{{end}}
  771. {{.ConfigHelp}}
  772. {{range .Commands}}
  773. {{.Desc}}
  774. Usage:
  775. {{indent .Usage "\t"}}
  776. {{end}}
  777. */
  778. package main
  779. `))
  780. func doHelpReal(ctx context.Context, args ...string) {
  781. if len(args) == 0 {
  782. usage(os.Stdout)
  783. return
  784. }
  785. for _, cmd := range commands {
  786. if cmd.Name == args[0] {
  787. fmt.Println(cmd.Usage)
  788. return
  789. }
  790. }
  791. log.Fatalf("Don't know command %q", args[0])
  792. }
  793. func doListInstances(ctx context.Context, args ...string) {
  794. if len(args) != 0 {
  795. log.Fatalf("usage: cbt listinstances")
  796. }
  797. is, err := getInstanceAdminClient().Instances(ctx)
  798. if err != nil {
  799. log.Fatalf("Getting list of instances: %v", err)
  800. }
  801. tw := tabwriter.NewWriter(os.Stdout, 10, 8, 4, '\t', 0)
  802. fmt.Fprintf(tw, "Instance Name\tInfo\n")
  803. fmt.Fprintf(tw, "-------------\t----\n")
  804. for _, i := range is {
  805. fmt.Fprintf(tw, "%s\t%s\n", i.Name, i.DisplayName)
  806. }
  807. tw.Flush()
  808. }
  809. func doListClusters(ctx context.Context, args ...string) {
  810. if len(args) != 0 {
  811. log.Fatalf("usage: cbt listclusters")
  812. }
  813. cis, err := getInstanceAdminClient().Clusters(ctx, config.Instance)
  814. if err != nil {
  815. log.Fatalf("Getting list of clusters: %v", err)
  816. }
  817. tw := tabwriter.NewWriter(os.Stdout, 10, 8, 4, '\t', 0)
  818. fmt.Fprintf(tw, "Cluster Name\tZone\tState\n")
  819. fmt.Fprintf(tw, "------------\t----\t----\n")
  820. for _, ci := range cis {
  821. fmt.Fprintf(tw, "%s\t%s\t%s (%d serve nodes)\n", ci.Name, ci.Zone, ci.State, ci.ServeNodes)
  822. }
  823. tw.Flush()
  824. }
  825. func doLookup(ctx context.Context, args ...string) {
  826. if len(args) < 2 {
  827. log.Fatalf("usage: cbt lookup <table> <row> [columns=<family:qualifier>...] [cells-per-column=<n>] " +
  828. "[app-profile=<app profile id>]")
  829. }
  830. parsed, err := parseArgs(args[2:], []string{"columns", "cells-per-column", "app-profile"})
  831. if err != nil {
  832. log.Fatal(err)
  833. }
  834. var opts []bigtable.ReadOption
  835. var filters []bigtable.Filter
  836. if cellsPerColumn := parsed["cells-per-column"]; cellsPerColumn != "" {
  837. n, err := strconv.Atoi(cellsPerColumn)
  838. if err != nil {
  839. log.Fatalf("Bad number of cells per column %q: %v", cellsPerColumn, err)
  840. }
  841. filters = append(filters, bigtable.LatestNFilter(n))
  842. }
  843. if columns := parsed["columns"]; columns != "" {
  844. columnFilters, err := parseColumnsFilter(columns)
  845. if err != nil {
  846. log.Fatal(err)
  847. }
  848. filters = append(filters, columnFilters)
  849. }
  850. if len(filters) > 1 {
  851. opts = append(opts, bigtable.RowFilter(bigtable.ChainFilters(filters...)))
  852. } else if len(filters) == 1 {
  853. opts = append(opts, bigtable.RowFilter(filters[0]))
  854. }
  855. table, row := args[0], args[1]
  856. tbl := getClient(bigtable.ClientConfig{AppProfile: parsed["app-profile"]}).Open(table)
  857. r, err := tbl.ReadRow(ctx, row, opts...)
  858. if err != nil {
  859. log.Fatalf("Reading row: %v", err)
  860. }
  861. printRow(r)
  862. }
  863. func printRow(r bigtable.Row) {
  864. fmt.Println(strings.Repeat("-", 40))
  865. fmt.Println(r.Key())
  866. var fams []string
  867. for fam := range r {
  868. fams = append(fams, fam)
  869. }
  870. sort.Strings(fams)
  871. for _, fam := range fams {
  872. ris := r[fam]
  873. sort.Sort(byColumn(ris))
  874. for _, ri := range ris {
  875. ts := time.Unix(0, int64(ri.Timestamp)*1e3)
  876. fmt.Printf(" %-40s @ %s\n", ri.Column, ts.Format("2006/01/02-15:04:05.000000"))
  877. fmt.Printf(" %q\n", ri.Value)
  878. }
  879. }
  880. }
  881. type byColumn []bigtable.ReadItem
  882. func (b byColumn) Len() int { return len(b) }
  883. func (b byColumn) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
  884. func (b byColumn) Less(i, j int) bool { return b[i].Column < b[j].Column }
  885. type byFamilyName []bigtable.FamilyInfo
  886. func (b byFamilyName) Len() int { return len(b) }
  887. func (b byFamilyName) Swap(i, j int) { b[i], b[j] = b[j], b[i] }
  888. func (b byFamilyName) Less(i, j int) bool { return b[i].Name < b[j].Name }
  889. func doLS(ctx context.Context, args ...string) {
  890. switch len(args) {
  891. default:
  892. log.Fatalf("Can't do `cbt ls %s`", args)
  893. case 0:
  894. tables, err := getAdminClient().Tables(ctx)
  895. if err != nil {
  896. log.Fatalf("Getting list of tables: %v", err)
  897. }
  898. sort.Strings(tables)
  899. for _, table := range tables {
  900. fmt.Println(table)
  901. }
  902. case 1:
  903. table := args[0]
  904. ti, err := getAdminClient().TableInfo(ctx, table)
  905. if err != nil {
  906. log.Fatalf("Getting table info: %v", err)
  907. }
  908. sort.Sort(byFamilyName(ti.FamilyInfos))
  909. tw := tabwriter.NewWriter(os.Stdout, 10, 8, 4, '\t', 0)
  910. fmt.Fprintf(tw, "Family Name\tGC Policy\n")
  911. fmt.Fprintf(tw, "-----------\t---------\n")
  912. for _, fam := range ti.FamilyInfos {
  913. fmt.Fprintf(tw, "%s\t%s\n", fam.Name, fam.GCPolicy)
  914. }
  915. tw.Flush()
  916. }
  917. }
  918. func doMDDocReal(ctx context.Context, args ...string) {
  919. data := map[string]interface{}{
  920. "Commands": commands,
  921. "Flags": docFlags(),
  922. "ConfigHelp": configHelp,
  923. }
  924. var buf bytes.Buffer
  925. if err := mddocTemplate.Execute(&buf, data); err != nil {
  926. log.Fatalf("Bad mddoc template: %v", err)
  927. }
  928. io.Copy(os.Stdout, &buf)
  929. }
  930. var mddocTemplate = template.Must(template.New("mddoc").Funcs(template.FuncMap{
  931. "indent": indentLines,
  932. }).
  933. Parse(`
  934. Cbt is a tool for doing basic interactions with Cloud Bigtable. To learn how to
  935. install the cbt tool, see the
  936. [cbt overview](https://cloud.google.com/bigtable/docs/cbt-overview).
  937. Usage:
  938. cbt [options] command [arguments]
  939. The commands are:
  940. {{range .Commands}}
  941. {{printf "%-25s %s" .Name .Desc}}{{end}}
  942. Use "cbt help <command>" for more information about a command.
  943. The options are:
  944. {{range .Flags}}
  945. -{{.Name}} string
  946. {{.Usage}}{{end}}
  947. {{.ConfigHelp}}
  948. {{range .Commands}}
  949. ## {{.Desc}}
  950. {{indent .Usage "\t"}}
  951. {{end}}
  952. `))
  953. func doRead(ctx context.Context, args ...string) {
  954. if len(args) < 1 {
  955. log.Fatalf("usage: cbt read <table> [args ...]")
  956. }
  957. parsed, err := parseArgs(args[1:], []string{
  958. "start", "end", "prefix", "columns", "count", "cells-per-column", "regex", "app-profile", "limit",
  959. })
  960. if err != nil {
  961. log.Fatal(err)
  962. }
  963. if _, ok := parsed["limit"]; ok {
  964. // Be nicer; we used to support this, but renamed it to "end".
  965. log.Fatal("Unknown arg key 'limit'; did you mean 'end'?")
  966. }
  967. if (parsed["start"] != "" || parsed["end"] != "") && parsed["prefix"] != "" {
  968. log.Fatal(`"start"/"end" may not be mixed with "prefix"`)
  969. }
  970. var rr bigtable.RowRange
  971. if start, end := parsed["start"], parsed["end"]; end != "" {
  972. rr = bigtable.NewRange(start, end)
  973. } else if start != "" {
  974. rr = bigtable.InfiniteRange(start)
  975. }
  976. if prefix := parsed["prefix"]; prefix != "" {
  977. rr = bigtable.PrefixRange(prefix)
  978. }
  979. var opts []bigtable.ReadOption
  980. if count := parsed["count"]; count != "" {
  981. n, err := strconv.ParseInt(count, 0, 64)
  982. if err != nil {
  983. log.Fatalf("Bad count %q: %v", count, err)
  984. }
  985. opts = append(opts, bigtable.LimitRows(n))
  986. }
  987. var filters []bigtable.Filter
  988. if cellsPerColumn := parsed["cells-per-column"]; cellsPerColumn != "" {
  989. n, err := strconv.Atoi(cellsPerColumn)
  990. if err != nil {
  991. log.Fatalf("Bad number of cells per column %q: %v", cellsPerColumn, err)
  992. }
  993. filters = append(filters, bigtable.LatestNFilter(n))
  994. }
  995. if regex := parsed["regex"]; regex != "" {
  996. filters = append(filters, bigtable.RowKeyFilter(regex))
  997. }
  998. if columns := parsed["columns"]; columns != "" {
  999. columnFilters, err := parseColumnsFilter(columns)
  1000. if err != nil {
  1001. log.Fatal(err)
  1002. }
  1003. filters = append(filters, columnFilters)
  1004. }
  1005. if len(filters) > 1 {
  1006. opts = append(opts, bigtable.RowFilter(bigtable.ChainFilters(filters...)))
  1007. } else if len(filters) == 1 {
  1008. opts = append(opts, bigtable.RowFilter(filters[0]))
  1009. }
  1010. // TODO(dsymonds): Support filters.
  1011. tbl := getClient(bigtable.ClientConfig{AppProfile: parsed["app-profile"]}).Open(args[0])
  1012. err = tbl.ReadRows(ctx, rr, func(r bigtable.Row) bool {
  1013. printRow(r)
  1014. return true
  1015. }, opts...)
  1016. if err != nil {
  1017. log.Fatalf("Reading rows: %v", err)
  1018. }
  1019. }
  1020. var setArg = regexp.MustCompile(`([^:]+):([^=]*)=(.*)`)
  1021. func doSet(ctx context.Context, args ...string) {
  1022. if len(args) < 3 {
  1023. log.Fatalf("usage: cbt set <table> <row> [app-profile=<app profile id>] family:[column]=val[@ts] ...")
  1024. }
  1025. var appProfile string
  1026. row := args[1]
  1027. mut := bigtable.NewMutation()
  1028. for _, arg := range args[2:] {
  1029. if strings.HasPrefix(arg, "app-profile=") {
  1030. appProfile = strings.Split(arg, "=")[1]
  1031. continue
  1032. }
  1033. m := setArg.FindStringSubmatch(arg)
  1034. if m == nil {
  1035. log.Fatalf("Bad set arg %q", arg)
  1036. }
  1037. val := m[3]
  1038. ts := bigtable.Now()
  1039. if i := strings.LastIndex(val, "@"); i >= 0 {
  1040. // Try parsing a timestamp.
  1041. n, err := strconv.ParseInt(val[i+1:], 0, 64)
  1042. if err == nil {
  1043. val = val[:i]
  1044. ts = bigtable.Timestamp(n)
  1045. }
  1046. }
  1047. mut.Set(m[1], m[2], ts, []byte(val))
  1048. }
  1049. tbl := getClient(bigtable.ClientConfig{AppProfile: appProfile}).Open(args[0])
  1050. if err := tbl.Apply(ctx, row, mut); err != nil {
  1051. log.Fatalf("Applying mutation: %v", err)
  1052. }
  1053. }
  1054. func doSetGCPolicy(ctx context.Context, args ...string) {
  1055. if len(args) < 3 {
  1056. log.Fatalf("usage: cbt setgcpolicy <table> <family> ((maxage=<d> | maxversions=<n>) [(and|or) (maxage=<d> | maxversions=<n>),...] | never)")
  1057. }
  1058. table := args[0]
  1059. fam := args[1]
  1060. pol, err := parseGCPolicy(strings.Join(args[2:], " "))
  1061. if err != nil {
  1062. log.Fatal(err)
  1063. }
  1064. if err := getAdminClient().SetGCPolicy(ctx, table, fam, pol); err != nil {
  1065. log.Fatalf("Setting GC policy: %v", err)
  1066. }
  1067. }
  1068. func doWaitForReplicaiton(ctx context.Context, args ...string) {
  1069. if len(args) != 1 {
  1070. log.Fatalf("usage: cbt waitforreplication <table>")
  1071. }
  1072. table := args[0]
  1073. fmt.Printf("Waiting for all writes up to %s to be replicated.\n", time.Now().Format("2006/01/02-15:04:05"))
  1074. if err := getAdminClient().WaitForReplication(ctx, table); err != nil {
  1075. log.Fatalf("Waiting for replication: %v", err)
  1076. }
  1077. }
  1078. func parseStorageType(storageTypeStr string) (bigtable.StorageType, error) {
  1079. switch storageTypeStr {
  1080. case "SSD":
  1081. return bigtable.SSD, nil
  1082. case "HDD":
  1083. return bigtable.HDD, nil
  1084. }
  1085. return -1, fmt.Errorf("Invalid storage type: %v, must be SSD or HDD", storageTypeStr)
  1086. }
  1087. func doCreateTableFromSnapshot(ctx context.Context, args ...string) {
  1088. if len(args) != 3 {
  1089. log.Fatal("usage: cbt createtablefromsnapshot <table> <cluster> <snapshot>")
  1090. }
  1091. tableName := args[0]
  1092. clusterName := args[1]
  1093. snapshotName := args[2]
  1094. err := getAdminClient().CreateTableFromSnapshot(ctx, tableName, clusterName, snapshotName)
  1095. if err != nil {
  1096. log.Fatalf("Creating table: %v", err)
  1097. }
  1098. }
  1099. func doSnapshotTable(ctx context.Context, args ...string) {
  1100. if len(args) != 3 && len(args) != 4 {
  1101. log.Fatal("usage: cbt createsnapshot <cluster> <snapshot> <table> [ttl=<d>]")
  1102. }
  1103. clusterName := args[0]
  1104. snapshotName := args[1]
  1105. tableName := args[2]
  1106. ttl := bigtable.DefaultSnapshotDuration
  1107. parsed, err := parseArgs(args[3:], []string{"ttl"})
  1108. if err != nil {
  1109. log.Fatal(err)
  1110. }
  1111. if val, ok := parsed["ttl"]; ok {
  1112. var err error
  1113. ttl, err = parseDuration(val)
  1114. if err != nil {
  1115. log.Fatalf("Invalid snapshot ttl value %q: %v", val, err)
  1116. }
  1117. }
  1118. err = getAdminClient().SnapshotTable(ctx, tableName, clusterName, snapshotName, ttl)
  1119. if err != nil {
  1120. log.Fatalf("Failed to create Snapshot: %v", err)
  1121. }
  1122. }
  1123. func doListSnapshots(ctx context.Context, args ...string) {
  1124. if len(args) != 0 && len(args) != 1 {
  1125. log.Fatal("usage: cbt listsnapshots [<cluster>]")
  1126. }
  1127. var cluster string
  1128. if len(args) == 0 {
  1129. cluster = "-"
  1130. } else {
  1131. cluster = args[0]
  1132. }
  1133. it := getAdminClient().Snapshots(ctx, cluster)
  1134. tw := tabwriter.NewWriter(os.Stdout, 10, 8, 4, '\t', 0)
  1135. fmt.Fprintf(tw, "Snapshot\tSource Table\tCreated At\tExpires At\n")
  1136. fmt.Fprintf(tw, "--------\t------------\t----------\t----------\n")
  1137. timeLayout := "2006-01-02 15:04 MST"
  1138. for {
  1139. snapshot, err := it.Next()
  1140. if err == iterator.Done {
  1141. break
  1142. }
  1143. if err != nil {
  1144. log.Fatalf("Failed to fetch snapshots %v", err)
  1145. }
  1146. fmt.Fprintf(tw, "%s\t%s\t%s\t%s\n", snapshot.Name, snapshot.SourceTable, snapshot.CreateTime.Format(timeLayout), snapshot.DeleteTime.Format(timeLayout))
  1147. }
  1148. tw.Flush()
  1149. }
  1150. func doGetSnapshot(ctx context.Context, args ...string) {
  1151. if len(args) != 2 {
  1152. log.Fatalf("usage: cbt getsnapshot <cluster> <snapshot>")
  1153. }
  1154. clusterName := args[0]
  1155. snapshotName := args[1]
  1156. snapshot, err := getAdminClient().SnapshotInfo(ctx, clusterName, snapshotName)
  1157. if err != nil {
  1158. log.Fatalf("Failed to get snapshot: %v", err)
  1159. }
  1160. timeLayout := "2006-01-02 15:04 MST"
  1161. fmt.Printf("Name: %s\n", snapshot.Name)
  1162. fmt.Printf("Source table: %s\n", snapshot.SourceTable)
  1163. fmt.Printf("Created at: %s\n", snapshot.CreateTime.Format(timeLayout))
  1164. fmt.Printf("Expires at: %s\n", snapshot.DeleteTime.Format(timeLayout))
  1165. }
  1166. func doDeleteSnapshot(ctx context.Context, args ...string) {
  1167. if len(args) != 2 {
  1168. log.Fatal("usage: cbt deletesnapshot <cluster> <snapshot>")
  1169. }
  1170. cluster := args[0]
  1171. snapshot := args[1]
  1172. err := getAdminClient().DeleteSnapshot(ctx, cluster, snapshot)
  1173. if err != nil {
  1174. log.Fatalf("Failed to delete snapshot: %v", err)
  1175. }
  1176. }
  1177. func doCreateAppProfile(ctx context.Context, args ...string) {
  1178. if len(args) < 4 || len(args) > 6 {
  1179. log.Fatal("usage: cbt createappprofile <instance-id> <profile-id> <description> " +
  1180. " (route-any | [ route-to=<cluster-id> : transactional-writes]) [optional flag] \n" +
  1181. "optional flags may be `force`")
  1182. }
  1183. routingPolicy, clusterID, err := parseProfileRoute(args[3])
  1184. if err != nil {
  1185. log.Fatalln("Exactly one of (route-any | [route-to : transactional-writes]) must be specified.")
  1186. }
  1187. config := bigtable.ProfileConf{
  1188. RoutingPolicy: routingPolicy,
  1189. InstanceID: args[0],
  1190. ProfileID: args[1],
  1191. Description: args[2],
  1192. }
  1193. opFlags := []string{"force", "transactional-writes"}
  1194. parseValues, err := parseArgs(args[4:], opFlags)
  1195. if err != nil {
  1196. log.Fatalf("optional flags can be specified as (force=<true>|transactional-writes=<true>) got %s ", args[4:])
  1197. }
  1198. for _, f := range opFlags {
  1199. fv, err := parseProfileOpts(f, parseValues)
  1200. if err != nil {
  1201. log.Fatalf("optional flags can be specified as (force=<true>|transactional-writes=<true>) got %s ", args[4:])
  1202. }
  1203. switch f {
  1204. case opFlags[0]:
  1205. config.IgnoreWarnings = fv
  1206. case opFlags[1]:
  1207. config.AllowTransactionalWrites = fv
  1208. default:
  1209. }
  1210. }
  1211. if routingPolicy == bigtable.SingleClusterRouting {
  1212. config.ClusterID = clusterID
  1213. }
  1214. profile, err := getInstanceAdminClient().CreateAppProfile(ctx, config)
  1215. if err != nil {
  1216. log.Fatalf("Failed to create app profile : %v", err)
  1217. }
  1218. fmt.Printf("Name: %s\n", profile.Name)
  1219. fmt.Printf("RoutingPolicy: %v\n", profile.RoutingPolicy)
  1220. }
  1221. func doGetAppProfile(ctx context.Context, args ...string) {
  1222. if len(args) != 2 {
  1223. log.Fatalln("usage: cbt getappprofile <instance-id> <profile-id>")
  1224. }
  1225. instanceID := args[0]
  1226. profileID := args[1]
  1227. profile, err := getInstanceAdminClient().GetAppProfile(ctx, instanceID, profileID)
  1228. if err != nil {
  1229. log.Fatalf("Failed to get app profile : %v", err)
  1230. }
  1231. fmt.Printf("Name: %s\n", profile.Name)
  1232. fmt.Printf("Etag: %s\n", profile.Etag)
  1233. fmt.Printf("Description: %s\n", profile.Description)
  1234. fmt.Printf("RoutingPolicy: %v\n", profile.RoutingPolicy)
  1235. }
  1236. func doListAppProfiles(ctx context.Context, args ...string) {
  1237. if len(args) != 1 {
  1238. log.Fatalln("usage: cbt listappprofile <instance-id>")
  1239. }
  1240. instance := args[0]
  1241. it := getInstanceAdminClient().ListAppProfiles(ctx, instance)
  1242. tw := tabwriter.NewWriter(os.Stdout, 10, 8, 4, '\t', 0)
  1243. fmt.Fprintf(tw, "AppProfile\tProfile Description\tProfile Etag\tProfile Routing Policy\n")
  1244. fmt.Fprintf(tw, "-----------\t--------------------\t------------\t----------------------\n")
  1245. for {
  1246. profile, err := it.Next()
  1247. if err == iterator.Done {
  1248. break
  1249. }
  1250. if err != nil {
  1251. log.Fatalf("Failed to fetch app profile %v", err)
  1252. }
  1253. fmt.Fprintf(tw, "%s\t%s\t%s\t%s\n", profile.Name, profile.Description, profile.Etag, profile.RoutingPolicy)
  1254. }
  1255. tw.Flush()
  1256. }
  1257. func doUpdateAppProfile(ctx context.Context, args ...string) {
  1258. if len(args) < 4 {
  1259. log.Fatal("usage: cbt updateappprofile <instance-id> <profile-id> <description>" +
  1260. " (route-any | [ route-to=<cluster-id> : transactional-writes]) [optional flag] \n" +
  1261. "optional flags may be `force`")
  1262. }
  1263. routingPolicy, clusterID, err := parseProfileRoute(args[3])
  1264. if err != nil {
  1265. log.Fatalln("Exactly one of (route-any | [route-to : transactional-writes]) must be specified.")
  1266. }
  1267. InstanceID := args[0]
  1268. ProfileID := args[1]
  1269. config := bigtable.ProfileAttrsToUpdate{
  1270. RoutingPolicy: routingPolicy,
  1271. Description: args[2],
  1272. }
  1273. opFlags := []string{"force", "transactional-writes"}
  1274. parseValues, err := parseArgs(args[4:], opFlags)
  1275. if err != nil {
  1276. log.Fatalf("optional flags can be specified as (force=<true>|transactional-writes=<true>) got %s ", args[4:])
  1277. }
  1278. for _, f := range opFlags {
  1279. fv, err := parseProfileOpts(f, parseValues)
  1280. if err != nil {
  1281. log.Fatalf("optional flags can be specified as (force=<true>|transactional-writes=<true>) got %s ", args[4:])
  1282. }
  1283. switch f {
  1284. case opFlags[0]:
  1285. config.IgnoreWarnings = fv
  1286. case opFlags[1]:
  1287. config.AllowTransactionalWrites = fv
  1288. default:
  1289. }
  1290. }
  1291. if routingPolicy == bigtable.SingleClusterRouting {
  1292. config.ClusterID = clusterID
  1293. }
  1294. err = getInstanceAdminClient().UpdateAppProfile(ctx, InstanceID, ProfileID, config)
  1295. if err != nil {
  1296. log.Fatalf("Failed to update app profile : %v", err)
  1297. }
  1298. }
  1299. func doDeleteAppProfile(ctx context.Context, args ...string) {
  1300. if len(args) != 2 {
  1301. log.Println("usage: cbt deleteappprofile <instance-id> <profile-id>")
  1302. }
  1303. err := getInstanceAdminClient().DeleteAppProfile(ctx, args[0], args[1])
  1304. if err != nil {
  1305. log.Fatalf("Failed to delete app profile : %v", err)
  1306. }
  1307. }
  1308. // parseDuration parses a duration string.
  1309. // It is similar to Go's time.ParseDuration, except with a different set of supported units,
  1310. // and only simple formats supported.
  1311. func parseDuration(s string) (time.Duration, error) {
  1312. // [0-9]+[a-z]+
  1313. // Split [0-9]+ from [a-z]+.
  1314. i := 0
  1315. for ; i < len(s); i++ {
  1316. c := s[i]
  1317. if c < '0' || c > '9' {
  1318. break
  1319. }
  1320. }
  1321. ds, u := s[:i], s[i:]
  1322. if ds == "" || u == "" {
  1323. return 0, fmt.Errorf("invalid duration %q", s)
  1324. }
  1325. // Parse them.
  1326. d, err := strconv.ParseUint(ds, 10, 32)
  1327. if err != nil {
  1328. return 0, fmt.Errorf("invalid duration %q: %v", s, err)
  1329. }
  1330. unit, ok := unitMap[u]
  1331. if !ok {
  1332. return 0, fmt.Errorf("unknown unit %q in duration %q", u, s)
  1333. }
  1334. if d > uint64((1<<63-1)/unit) {
  1335. // overflow
  1336. return 0, fmt.Errorf("invalid duration %q overflows", s)
  1337. }
  1338. return time.Duration(d) * unit, nil
  1339. }
  1340. var unitMap = map[string]time.Duration{
  1341. "ms": time.Millisecond,
  1342. "s": time.Second,
  1343. "m": time.Minute,
  1344. "h": time.Hour,
  1345. "d": 24 * time.Hour,
  1346. }
  1347. func doVersion(ctx context.Context, args ...string) {
  1348. fmt.Printf("%s %s %s\n", version, revision, revisionDate)
  1349. }
  1350. // parseArgs takes a slice of arguments of the form key=value and returns a map from
  1351. // key to value. It returns an error if an argument is malformed or a key is not in
  1352. // the valid slice.
  1353. func parseArgs(args []string, valid []string) (map[string]string, error) {
  1354. parsed := make(map[string]string)
  1355. for _, arg := range args {
  1356. i := strings.Index(arg, "=")
  1357. if i < 0 {
  1358. return nil, fmt.Errorf("Bad arg %q", arg)
  1359. }
  1360. key, val := arg[:i], arg[i+1:]
  1361. if !stringInSlice(key, valid) {
  1362. return nil, fmt.Errorf("Unknown arg key %q", key)
  1363. }
  1364. parsed[key] = val
  1365. }
  1366. return parsed, nil
  1367. }
  1368. func stringInSlice(s string, list []string) bool {
  1369. for _, e := range list {
  1370. if s == e {
  1371. return true
  1372. }
  1373. }
  1374. return false
  1375. }
  1376. func parseColumnsFilter(columns string) (bigtable.Filter, error) {
  1377. splitColumns := strings.FieldsFunc(columns, func(c rune) bool { return c == ',' })
  1378. if len(splitColumns) == 1 {
  1379. filter, err := columnFilter(splitColumns[0])
  1380. if err != nil {
  1381. return nil, err
  1382. }
  1383. return filter, nil
  1384. }
  1385. var columnFilters []bigtable.Filter
  1386. for _, column := range splitColumns {
  1387. filter, err := columnFilter(column)
  1388. if err != nil {
  1389. return nil, err
  1390. }
  1391. columnFilters = append(columnFilters, filter)
  1392. }
  1393. return bigtable.InterleaveFilters(columnFilters...), nil
  1394. }
  1395. func columnFilter(column string) (bigtable.Filter, error) {
  1396. splitColumn := strings.Split(column, ":")
  1397. if len(splitColumn) == 1 {
  1398. return bigtable.ColumnFilter(splitColumn[0]), nil
  1399. } else if len(splitColumn) == 2 {
  1400. if strings.HasSuffix(column, ":") {
  1401. return bigtable.FamilyFilter(splitColumn[0]), nil
  1402. } else if strings.HasPrefix(column, ":") {
  1403. return bigtable.ColumnFilter(splitColumn[1]), nil
  1404. } else {
  1405. familyFilter := bigtable.FamilyFilter(splitColumn[0])
  1406. qualifierFilter := bigtable.ColumnFilter(splitColumn[1])
  1407. return bigtable.ChainFilters(familyFilter, qualifierFilter), nil
  1408. }
  1409. } else {
  1410. return nil, fmt.Errorf("Bad format for column %q", column)
  1411. }
  1412. }
  1413. func parseProfileRoute(str string) (routingPolicy, clusterID string, err error) {
  1414. route := strings.Split(str, "=")
  1415. switch route[0] {
  1416. case "route-any":
  1417. if len(route) > 1 {
  1418. err = fmt.Errorf("got %v", route)
  1419. break
  1420. }
  1421. routingPolicy = bigtable.MultiClusterRouting
  1422. case "route-to":
  1423. if len(route) != 2 || route[1] == "" {
  1424. err = fmt.Errorf("got %v", route)
  1425. break
  1426. }
  1427. routingPolicy = bigtable.SingleClusterRouting
  1428. clusterID = route[1]
  1429. default:
  1430. err = fmt.Errorf("got %v", route)
  1431. }
  1432. return
  1433. }
  1434. func parseProfileOpts(opt string, parsedArgs map[string]string) (bool, error) {
  1435. if val, ok := parsedArgs[opt]; ok {
  1436. status, err := strconv.ParseBool(val)
  1437. if err != nil {
  1438. return false, fmt.Errorf("expected %s = <true> got %s ", opt, val)
  1439. }
  1440. return status, nil
  1441. }
  1442. return false, nil
  1443. }